Open In App

Structured Query Language (SQL)

Last Updated : 04 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Structured Query Language is a standard Database language that is used to create, maintain, and retrieve the relational database. In this article, we will discuss this in detail about SQL. Following are some interesting facts about SQL. Let’s focus on that.

SQL is case insensitive. But it is a recommended practice to use keywords (like SELECT, UPDATE, CREATE, etc.) in capital letters and use user-defined things (like table name, column name, etc.) in small letters.

We can write comments in SQL using “–” (double hyphen) at the beginning of any line. SQL is the programming language for relational databases (explained below) like MySQL, Oracle, Sybase, SQL Server, Postgre, etc. Other non-relational databases (also called NoSQL) databases like MongoDB, DynamoDB, etc. do not use SQL.

Although there is an ISO standard for SQL, most of the implementations slightly vary in syntax. So we may encounter queries that work in SQL Server but do not work in MySQL.

What is Relational Database?

A relational database means the data is stored as well as retrieved in the form of relations (tables). Table 1 shows the relational database with only one relation called STUDENT which stores ROLL_NO, NAME, ADDRESS, PHONE, and AGE of students.

STUDENT Table

ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 9156768971 18

Important Terminologies

These are some important terminologies that are used in terms of relation.

  • Attribute:Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME etc.
  • Tuple:Each row in the relation is known as tuple. The above relation contains 4 tuples, one of which is shown as:

1 RAM DELHI 9455123451 18
  • Degree:The number of attributes in the relation is known as degree of the relation. The STUDENT relation defined above has degree 5.
  • Cardinality:The number of tuples in a relation is known as cardinality. The STUDENTrelation defined above has cardinality 4.
  • Column:Column represents the set of values for a particular attribute. The column ROLL_NO is extracted from relation STUDENT.
ROLL_NO
1
2
3
4

How Queries can be Categorized in Relational Database?

The queries to deal with relational database can be categorized as:

  • Data Definition Language:It is used to define the structure of the database. e.g; CREATE TABLE, ADD COLUMN, DROP COLUMN and so on.
  • Data Manipulation Language:It is used to manipulate data in the relations. e.g.; INSERT, DELETE, UPDATE and so on.
  • Data Query Language:It is used to extract the data from the relations. e.g.; SELECT So first we will consider the Data Query Language. A generic query to retrieve data from a relational database is:
  1. SELECT [DISTINCT] Attribute_List FROM R1,R2….RM
  2. [WHERE condition]
  3. [GROUP BY (Attributes)[HAVING condition]]
  4. [ORDER BY(Attributes)[DESC]];

Part of the query represented by statement 1 is compulsory if you want to retrieve from a relational database. The statements written inside [] are optional. We will look at the possible query combination on relation shown in Table 1.

Different Query Combinations

Case 1: If we want to retrieve attributes ROLL_NO and NAMEof all students, the query will be:

SELECT ROLL_NO, NAME FROM STUDENT;

ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
4 SURESH

Case 2: If we want to retrieve ROLL_NO and NAME of the students whose ROLL_NO is greater than 2, the query will be:

SELECT ROLL_NO, NAME FROM STUDENT 
WHERE ROLL_NO>2;

ROLL_NO NAME
3 SUJIT
4 SURESH

CASE 3: If we want to retrieve all attributes of students, we can write * in place of writing all attributes as:

SELECT * FROM STUDENT 
WHERE ROLL_NO>2;

ROLL_NO NAME ADDRESS PHONE AGE
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 9156768971 18

CASE 4: If we want to represent the relation in ascending order by AGE, we can use ORDER BY clause as:

SELECT * FROM STUDENT ORDER BY AGE;

ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
4 SURESH DELHI 9156768971 18
3 SUJIT ROHTAK 9156253131 20

Note:

ORDER BY AGEis equivalent to ORDER BY AGE ASC. 
If we want to retrieve the results in descending order of AGE, we can use ORDER BY AGE DESC.

CASE 5: If we want to retrieve distinct values of an attribute or group of attribute, DISTINCT is used as in:

SELECT DISTINCT ADDRESS FROM STUDENT;

ADDRESS
DELHI
GURGAON
ROHTAK

If DISTINCT is not used, DELHI will be repeated twice in result set. Before understanding GROUP BY and HAVING, we need to understand aggregations functions in SQL.

Aggregation Functions

Aggregation functions are used to perform mathematical operations on data values of a relation. Some of the common aggregation functions used in SQL are:

  • COUNT: Count function is used to count the number of rows in a relation. e.g;
SELECT COUNT (PHONE) FROM STUDENT;

COUNT(PHONE)
4
  • SUM: SUM function is used to add the values of an attribute in a relation. e.g;
SELECT SUM(AGE) FROM STUDENT; 

SUM(AGE)
74

In the same way, MIN, MAX and AVG can be used.  As we have seen above, all aggregation functions return only 1 row. AVERAGE: It gives the average values of the tupples. It is also defined as sum divided by count values.

Syntax:

AVG(attributename)

OR

SUM(attributename)/COUNT(attributename)

The above mentioned syntax also retrieves the average value of tupples.

  • MAXIMUM:It extracts the maximum value among the set of tupples.

Syntax:

MAX(attributename)

  • MINIMUM:It extracts the minimum value amongst the set of all the tupples.

Syntax:

MIN(attributename)

  • GROUP BY:Group by is used to group the tuples of a relation based on an attribute or group of attribute. It is always combined with aggregation function which is computed on group. e.g.;
SELECT ADDRESS, SUM(AGE) FROM STUDENT
GROUP BY (ADDRESS);

In this query, SUM(AGE) will be computed but not for entire table but for each address. i.e.; sum of AGE for address DELHI(18+18=36) and similarly for other address as well. The output is:

ADDRESS SUM(AGE)
DELHI 36
GURGAON 18
ROHTAK 20

If we try to execute the query given below, it will result in error because although we have computed SUM(AGE) for each address, there are more than 1 ROLL_NO for  each address we have grouped. So it can’t be displayed in result set. We need to use aggregate functions on columns after SELECT statement to make sense of the resulting set whenever we are using GROUP BY.

SELECT ROLL_NO, ADDRESS, SUM(AGE) FROM STUDENT
GROUP BY (ADDRESS);

NOTE:

An attribute which is not a part of GROUP BY clause can’t be used for selection. 
Any attribute which is part of GROUP BY CLAUSE can be used for selection but it is not mandatory.
But we could use attributes which are not a part of the GROUP BY clause in an aggregate function.

Conclusion

SQL is a standard language used to manage data in relational databases. SQL is mainly divided into four main categories: Data definition language, data manipulation language, transaction control language, and data query language and SQL is a powerful language that can be used to carry out a wide range of operations like insert ,delete and update.



Previous Article
Next Article

Similar Reads

Features of Structured Query Language (SQL)
Here are some key features of Structured Query Language (SQL): Data Definition Language (DDL): SQL provides a set of commands to define and modify the structure of a database, including creating tables, modifying table structure, and dropping tables.Data Manipulation Language (DML): SQL provides a set of commands to manipulate data within a databas
4 min read
Difference between Structured, Semi-structured and Unstructured data
Big Data includes huge volume, high velocity, and extensible variety of data. There are 3 types: Structured data, Semi-structured data, and Unstructured data. Structured data - Structured data is data whose elements are addressable for effective analysis. It has been organized into a formatted repository that is typically a database. It concerns al
2 min read
What is Structured Data?
Structured data refers to data that is organized and formatted in a specific way to make it easily readable and understandable by both humans and machines. This is typically achieved through the use of a well-defined schema or data model, which provides a structure for the data. Structured data is typically found in databases and spreadsheets, and
5 min read
What is Semi-structured data?
Semi-structured data is a type of data that is not purely structured, but also not completely unstructured. It contains some level of organization or structure, but does not conform to a rigid schema or data model, and may contain elements that are not easily categorized or classified. Semi-structured data is typically characterized by the use of m
7 min read
Large objects(LOBs) for Semi Structured and Unstructured Data
Large objects (LOBs) are a type of data type used to store semi-structured and unstructured data in a database. LOBs are typically used for storing data that is too large to fit into a traditional data type, such as text documents, images, videos, and audio files. LOBs are particularly useful for storing semi-structured and unstructured data, as th
5 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL vs NO SQL vs NEW SQL
SQL stands for Structured Query Language. Which is based on relational algebra and schema is fixed in this which means data is stored in the form of columns and tables. SQL follows ACID properties which means Atomicity, Consistency, Isolation, and Durability are maintained. There are three types of languages present in SQL : Data Definition Languag
2 min read
Neo4j Query Cypher Language
The Neo4j has its own query language that called Cypher Language. It is similar to SQL, remember one thing Neo4j does not work with tables, row or columns it deals with nodes. It is more satisfied to see the data in a graph format rather than in a table format. Example: The Neo4j Cypher statement compare to SQL MATCH (G:Company { name:"GeeksforGeek
2 min read
Database Roles in CQL (Cassandra Query Language)
Cassandra Query Language (CQL) is the query language used to interact with Cassandra databases. Unlike traditional relational databases, Cassandra does not have a built-in concept of database roles. Instead, access control is managed at the object level, such as the keyspace, table, or column level. Cassandra provides a set of permissions that can
5 min read
Additional Functions in CQL (Cassandra Query Language)
In this article, we will discuss several functions that are supported by CQL in Cassandra which can help in converting one value type into another new value directly by using the function. In Cassandra, there are several functions that are supported by CQL which helps in many ways such that there is a scenario where we want to find the TTL value of
3 min read
Article Tags :