SQL
SQL
SQL stands for Structured Query Language. It is a standardized programming
language used to manage and manipulate relational databases. It enables users to
perform a variety of tasks such as querying data, creating and modifying database
structures, and managing access permissions. SQL is widely used across various
relational database management systems such as MySQL, PostgreSQL, Oracle, and
SQL Server.
History of SQL
SQL was invented in 1970s and was first commercially distributed by Oracle.
The original name was given by IBM as Structured English Query Language,
abbreviated by the acronym SEQUEL.
Why we use SQL
Scalability: Handles huge datasets (millions of rows) effortlessly
Reliability: Ensures data consistency via ACID principles
Flexibility: Supports complex joins, aggregations, and data manipulation
Integration: Easily plugs into tools like Excel, Python, Power BI, Tableau
SQL is widely popular because it offers the following advantages −
Allows users to access data in the relational database management systems.
Allows users to describe the data.
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-
compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views.
Components of a SQL System
A SQL system consists of several key components that work together to enable
efficient data storage, retrieval, and manipulation. Some of the Key components of a
SQL System are:
Databases: Databases are structured collections of data organized into tables, rows, and
columns. Databases serve as repositories for storing information efficiently and
provide a way to manage and access data.
Tables: Tables are the fundamental building blocks of a database, consisting of rows
(records) and columns (attributes or fields). Tables ensure data integrity and
consistency by defining the structure and relationships of the stored information.
Queries: Queries are SQL commands used to interact with databases. They enable
users to retrieve, update, insert, or delete data from tables, allowing for efficient data
manipulation and retrieval.
Constraints: Constraints are rules applied to tables to maintain data integrity.
Constraints define conditions that data must meet to be stored in the database, ensuring
accuracy and consistency.
Stored Procedures: Stored procedures are pre-compiled SQL statements stored in the
database. Stored procedures can accept parameters, execute complex operations, and
return results, enhancing efficiency, reusability, and security in database management.
Transactions: Transactions are groups of SQL statements that are executed as a single
unit of work. Transactions ensure data consistency and integrity by allowing for the
rollback of changes if any part of the transaction fails.
Characteristics of SQL?
User-Friendly and Accessible: SQL is designed for a broad range of users,
including those with minimal programming experience, making it approachable
for non-technical individuals.
Declarative Language: As a non-procedural language, SQL allows users to
specify what data is needed rather than how to retrieve it, focusing on the desired
results rather than the retrieval process.
Efficient Database Management: SQL enables the creation, modification, and
management of databases efficiently, saving time and simplifying complex
database operations.
Standardized Language: Based on ANSI (American National Standards
Institute) and ISO (International Organization for Standardization) standards,
SQL ensures consistency and stability across various database management
systems (DBMS).
Command Structure: SQL does not require a continuation character for multi-
line queries, allowing flexibility in writing commands across one or multiple
lines.
Execution Mechanism: Queries are executed using a termination character (e.g.,
a semicolon ;), enabling immediate and accurate command processing.
Built-in Functionality: SQL includes a rich set of built-in functions for data
manipulation, aggregation, and formatting, empowering users to handle diverse
data-processing needs effectively.
Industry Applications
Finance & E-Commerce
Transactional systems (e.g., banks, online retailers) store customer
transactions in relational databases. SQL generates reports like net revenue,
expense analyses, and risk-weighted statistical summaries
Big retailers (e.g., Walmart) process over a million transactions per hour into
petabyte-scale systems for trend analysis and forecasting .
Healthcare
Electronic Health Records (EHRs) leverage SQL to query patient data,
monitor outcomes, and generate dashboards for doctors and administrators
Marketing & Social Media
Marketers use SQL for campaign analysis, A/B tests, customer segmentation
(CLV, CAC), and behavioral pattern detection
Platforms like Facebook, Instagram, Twitter rely on SQL to store user profiles,
posts, activity logs, and to generate engagement metrics
Streaming & Entertainment
Services like Spotify, Amazon Prime, Disney+ use SQL to catalog media,
store playback data, and generate personalized recommendations .
Cybersecurity
Analysts query SQL database logs to detect threats, investigate incidents,
manage vulnerabilities, and monitor compliance metrics
Back-end Development & Data Science
SQL is the backbone of CRUD operations in web apps—creating, retrieving,
updating, deleting data. It's used for schema definition, table creation, and
joining multiple tables .
Data scientists use SQL for data cleaning, deduplication, aggregation (SUM,
AVG, COUNT), prepping data for machine learning models
Insights from Real Users
From a reddit discussion on SQL use cases:
“SQL can handle much larger volumes of data and is very easy to
navigate… within a week of using SQL I was able to analyze data
much more in depth than I could just using Excel.”
“I have tables with hundreds of millions of rows of data… I can run
aggregates across hundreds of gigabytes…”
These reflect how SQL empowers high-volume data analysis that spreadsheets can’t
handle.
Common SQL Tasks (Indeed)
Table creation & schema design (CREATE, ALTER)
Inserting, updating, deleting records (INSERT, UPDATE, DELETE)
Data retrieval (SELECT, often with WHERE, JOIN, GROUP BY, HAVING)
Data access control (GRANT, REVOKE)
Procedures & functions for encapsulated logic
Advanced SQL Techniques
Top-N per group situations (e.g., find top 3 customers per region) using
OUTER/CROSS APPLY in SQL Server
Query cost estimation & optimization at Twitter—using ML to predict
CPU/memory usage of SQL queries
Context SQL Use Case Example
Retail & Finance Transaction logs, big-data trends
Healthcare Patient management, clinical reporting
Marketing Segmentation, A/B testing
Streaming Services Recommendations, content catalogs
Back-end Systems APIs, web applications
Data Science Aggregation, ETL, ML prep
Security Threat detection, audit log analysis
In short, anywhere structured data is stored, SQL is involved—under the hood of
apps we use every day.
How SQL Works?
Structured Query Language (SQL) operates on a server machine, where it processes
database queries and returns results efficiently. Below are the key software
components involved in the SQL execution process.
1. Input: The process begins when a user submits an SQL query through a database
interface or application. This query typically specifies the desired operation, such
as data retrieval, insertion, updating, or deletion.
2. Parsing: The query is passed to the query processor, which breaks it into smaller
units called tokens. These tokens represent keywords, table names, column
names, and other elements of the query. The processor then validates the syntax
against SQL standards and the database schema to ensure the query is well-
formed and executable.
3. Optimization: After parsing, the query is handed to the optimizer, which
evaluates multiple ways to execute the query. The optimizer considers factors like
indexes, table statistics, and available resources to generate the most efficient
execution plan. This step ensures that the query runs with minimal resource
consumption and maximum performance.
4. Execution: The execution engine follows the plan provided by the optimizer. It
interacts with the storage engine, which retrieves, manipulates, or updates the
required data from the database tables. During this step, SQL statements like
SELECT, INSERT, UPDATE, or DELETE are translated into actions performed
on the underlying data.
5. Output: Once the execution engine processes the query, the result is formatted
and returned to the user. Depending on the query type, the output could be a result
set (for SELECT queries) or an acknowledgment of the operation (for INSERT,
UPDATE, or DELETE queries).
By combining these steps, SQL ensures the seamless interaction between users and
relational databases, enabling efficient data manipulation and retrieval.
Rules for Writing SQL Queries
There are certain rules for SQL which would ensure consistency and functionality
across databases. By following these rules, queries will be well formed and well
executed in any database.
Statement Termination: Every SQL statement ends with a semicolon (;),
signaling the DBMS to execute the command.
Case Insensitivity: SQL keywords (e.g., SELECT, INSERT) are case-insensitive,
but database names and column names may be case-sensitive depending on the
DBMS.
Whitespace Flexibility: SQL statements can span multiple lines, but keywords
and identifiers must be separated by at least one space.
Unique Identifiers: Reserved words (e.g., SELECT, FROM) cannot be used as
table or column names unless enclosed in double quotes (") or backticks (`),
depending on the DBMS.
Comments: Comments enhance readability:
o Single-line comments: --
o Multi-line comments: /* ... */
Data Integrity: Constraints like NOT NULL, UNIQUE, and PRIMARY KEY
must be defined correctly to maintain data consistency.
String Literals: String values must be enclosed in single quotes (').
Valid Identifiers: Table and column names must:
o Begin with an alphabetic character.
o Contain up to 30 characters.
o Avoid special characters except underscores (_).
By following these rules, SQL users ensure reliable query execution and
maintainable database structures.
What is Database?
A database is an organized collection of data stored in a computer
system and usually managed by a database management system
(DBMS).
The most commonly used types of databases are Relational and
NoSQL.
The data in relational databases is modeled in tables, making
querying and processing efficient. Structured query language (SQL)
is commonly used for data querying and writing.
The data in NoSQL databases is modeled as key-value pairs.
Examples of relational DBMS are MySQL, Oracle, Microsoft SQL
Server and PostgreSQL.
Examples of NoSQL DBMS are MongoDB, Cassandra and
DynamoDB
Applications of Databases:
Databases are essential part of our life. We encounter several
activities that involve our interaction with databases, for example in
the bank, in the railway station, in school, in a grocery store, etc.
Most of the applications that we use in our day to day life (like
WhatsApp, Gmail, Social Media Websites, etc) have a database that
stores user, transaction and all other required information in a
database.
New media technology has made it possible to store images, video
clips. These essential features are making multimedia databases.
Databases are backbone for any business as they also allow
businesses to do data analysis and apply data science to take
business decisions.
Relational Databases:
A relational database’s contents are arranged as a collection of
tables with rows and columns. Accessing structured data is made
most flexible and efficient by relational database technology.
What is DBMS?
Collection of interrelated data and set of programs to access data
example – MySql, Oracle.
An interface for operations like creation, deletion, modification, etc
is provided by DBMS.
DBMS allows the user to create their databases as per their
requirement.
DBMS accepts the request from the application and provides
specific data through the operating system.
DBMS contains a group of programs that acts according to the
user’s instruction.
It provides security to the database.
SQL Commands
SQL Commands are like instructions to a table. It is used to interact
with the database with some operations. It is also used to perform
specific tasks, functions, and queries of data. SQL can perform
various tasks like creating a table, adding data to tables, dropping
the table, modifying the table, set permission for users.
SQL Commands are mainly categorized into five categories:
DDL – Data Definition Language
DQL – Data Query Language
DML – Data Manipulation Language
DCL – Data Control Language
TCL – Transaction Control Language
Data Definition Language (DDL):
DDL or Data Definition Language actually consists of the SQL
commands that can be used to defining, altering, and deleting
database structures such as tables, indexes, and schemas. It simply
deals with descriptions of the database schema and is used to
create and modify the structure of database objects in the database
Common DDL Commands
Command Description Syntax
Create database or
its objects (table, CREATE TABLE
index, function, table_name (column1
CREATE
views, store data_type, column2
procedure, and data_type, ...);
triggers)
Delete objects from DROP TABLE
DROP
the database table_name;
Command Description Syntax
ALTER TABLE
table_name ADD
Alter the structure
ALTER COLUMN
of the database
column_name
data_type;
Remove all records
from a table,
TRUNCATE TABLE
TRUNCATE including all spaces
table_name;
allocated for the
records are removed
Rename an object RENAME TABLE
RENAME existing in the old_table_name TO
database new_table_name;
Example of DDL:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);
Data Query Language (DQL)
DQL statements are used for performing queries on the data within
schema objects. The purpose of the DQL Command is to get some
schema relation based on the query passed to it. This command
allows getting the data out of the database to perform operations
with it. When a SELECT is fired against a table or tables the result is compiled into a
further temporary table, which is displayed or perhaps received by the program.
DQL Command
Comman Description Syntax
d
It is used to retrieve data SELECT column1, column2, ...FROM
SELECT
from the database table_name WHERE condition;
Data Manipulation Language (DML)
The SQL commands that deal with the manipulation of data present in the database
belong to DML or Data Manipulation Language and this includes most of the SQL
statements. It is the component of the SQL statement that controls access to data and to
the database. Basically, DCL statements are grouped with DML statements.
Common DML Commands
Command Description Syntax
Insert data into a INSERT INTO table_name (column1,
INSERT
table column2, ...) VALUES (value1, value2, ...);
Update existing data UPDATE table_name SET column1 = value1,
UPDATE
within a table column2 = value2 WHERE condition;
Delete records from a DELETE FROM table_name WHERE
DELETE
database table condition;
Example of DML
INSERT INTO employees (first_name, last_name, department)
VALUES ('Jane', 'Smith', 'HR');
Data Control Language (DCL)
DCL (Data Control Language) includes commands such as GRANT and REVOKE
which mainly deal with the rights, permissions, and other controls of the database
system. These commands are used to control access to data in the database by granting
or revoking permissions.
Common DCL Commands
Command Description Syntax
Assigns new privileges
to a user account, GRANT privilege_type
allowing access to [(column_list)] ON [object_type]
GRANT
specific database object_name TO user [WITH
objects, actions, or GRANT OPTION];
functions.
Removes previously
granted privileges REVOKE [GRANT OPTION
from a user account, FOR] privilege_type
REVOKE taking away their [(column_list)] ON [object_type]
access to certain object_name FROM user
database objects or [CASCADE];
actions.
Example of DCL:
GRANT SELECT, UPDATE ON employees TO user_name;
Transaction Control Language (TCL)
Transactions group a set of tasks into a single execution unit. Each transaction begins
with a specific task and ends when all the tasks in the group are successfully
completed. If any of the tasks fail, the transaction fails. Therefore, a transaction has
only two results: success or failure.
Common TCL Commands
Command Description Syntax
BEGIN BEGIN TRANSACTION
Starts a new transaction
TRANSACTION [transaction_name];
Saves all changes made
COMMIT COMMIT;
during the transaction
Undoes all changes made
ROLLBACK ROLLBACK;
during the transaction
Creates a savepoint within SAVEPOINT
SAVEPOINT
the current transaction savepoint_name;
Example of TCL
BEGIN TRANSACTION;
UPDATE employees SET department = 'Marketing' WHERE department = 'Sales';
SAVEPOINT before_update;
UPDATE employees SET department = 'IT' WHERE department = 'HR';
ROLLBACK TO SAVEPOINT before_update;
COMMIT;
SQL Clauses
WHERE Clause
The SQL WHERE clause allows to filtering of records in queries. Whether you’re
retrieving data, updating records, or deleting entries from a database, the WHERE
clause plays an important role in defining which rows will be affected by the query.
Without it, SQL queries would return all rows in a table, making it difficult to target
specific data.
The SQL WHERE clause is used to specify a condition while fetching or modifying
data in a database. It filters the rows that are affected by the SELECT, UPDATE,
DELETE, or INSERT operations.
The condition can range from simple comparisons to complex expressions, enabling
precise targeting of the data.
Syntax:
SELECT column1,column2 FROM table_name WHERE column_name operator
value;
Examples of WHERE Clause in SQL
We will create a basic employee table structure in SQL for performing all the where
clause operation.
Query:
CREATE TABLE Emp1(
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Country VARCHAR(50),
Age int(2),
mob int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Emp1 (EmpID, Name,Country, Age, mob)
VALUES (1, 'Shubham', 'India','23','738479734'),
(2, 'Aman ', 'Australia','21','436789555'),
(3, 'Naveen', 'Sri lanka','24','34873847'),
(4, 'Aditya', 'Austria','21','328440934'),
(5, 'Nishant', 'Spain','22','73248679');
Output:
EmpID Name Country Age mob
Shubha 73847973
1 India 23
m 4
Australi 43678955
2 Aman 21
a 5
3 Naveen Sri lanka 24 34873847
32844093
4 Aditya Austria 21
4
5 Nishant Spain 22 73248679
Example 1: Where Clause with Logical Operators
To fetch records of Employee with age equal to 24.
Query:
SELECT * FROM Emp1 WHERE Age=24;
Output:
To fetch the EmpID, Name and Country of Employees with Age greater than 21.
Query:
SELECT EmpID, Name, Country FROM Emp1 WHERE Age > 21;
Output:
Example 2: Where Clause with BETWEEN Operator
It is used to fetch filtered data in a given range inclusive of two values.
Syntax:
SELECT column1,column2 FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Parameter Explanation:
BETWEEN: operator name
value1 AND value2: exact value from value1 to value2 to get related data in result set.
To fetch records of Employees where Age is between 22 and 24 (inclusive).
Query:
SELECT * FROM Emp1 WHERE Age BETWEEN 22 AND 24;
Output:
Example 3: Where Clause with LIKE Operator
It is used to fetch filtered data by searching for a particular pattern in the where clause.
Syntax:
SELECT column1,column2 FROM
table_name WHERE column_name LIKE pattern;
Parameters Explanation:
LIKE: operator name
pattern: exact value extracted from the pattern to get related data in the result set.
Note: The character(s) in the pattern is case-insensitive.
To fetch records of Employees where Name starts with the letter S.
Query:
SELECT * FROM Emp1 WHERE Name LIKE 'S%';
The ‘%'(wildcard) signifies the later characters here which can be of any length and
value.
Output:
To fetch records of Employees where Name contains the pattern ‘M’.
Query:
SELECT * FROM Emp1 WHERE Name LIKE '%M%';
Output:
Example 4: Where Clause with IN Operator
It is used to fetch the filtered data same as fetched by ‘=’ operator just the difference is
that here we can specify multiple values for which we can get the result set.
Syntax:
SELECT column1,column2 FROM table_name WHERE column_name IN
(value1,value2,..);
Parameters Explanation:
IN: operator name
value1,value2,..: exact value matching the values given and get related data in the
result set.
To fetch the Names of Employees where Age is 21 or 23.
Query:
SELECT Name FROM Emp1 WHERE Age IN (21,23);
Output:
HAVING Clause
The HAVING clause in SQL is used to filter query results based on aggregate
functions. Unlike the WHERE clause, which filters individual rows before grouping,
the HAVING clause filters groups of data after aggregation. It is commonly used with
functions like SUM(), AVG(), COUNT(), MAX(), and MIN().
The HAVING clause is used to filter the result of the GROUP BY statement based on
the specified conditions. It allows filtering grouped data using Boolean conditions
(AND, OR). It was introduced because the WHERE clause cannot be used with
aggregate functions. Similar to WHERE clause, it helps apply conditions but
specifically works with grouped data. When we need to filter aggregated results, the
HAVING clause is the appropriate choice.
Key Features of the HAVING Clause
Used to filter grouped data based on aggregate functions.
Works with Boolean conditions (AND, OR
Cannot be used without GROUP BY unless an aggregate function is present.
Must be placed after the GROUP BY clause and before the ORDER BY clause (if
used).
Helps generate summary reports from large datasets.
Syntax:
SELECT column_name, AGGREGATE_FUNCTION(column_name)
FROM table_name
GROUP BY column_name
HAVING condition;
Here, the function_name is the name of the function used, for example, SUM(), and
AVG().
SQL HAVING Clause Examples
Here first we create a database name as “Company”, then we will create a table named
“Employee” in the database. After creating a table we will execute the query.
Query:
-- Create the Employee table with appropriate data types
CREATE TABLE Employee (
EmployeeId int,
Name varchar(50),
Gender varchar(10),
Salary int,
Department varchar(20),
Experience int -- Changed to int for years of experience
);
-- Insert multiple rows into the Employee table in a single query
INSERT INTO Employee (EmployeeId, Name, Gender, Salary, Department,
Experience)
VALUES
(5, 'Priya Sharma', 'Female', 45000, 'IT', 2),
(6, 'Rahul Patel', 'Male', 65000, 'Sales', 5),
(7, 'Nisha Gupta', 'Female', 55000, 'Marketing', 4),
(8, 'Vikram Singh', 'Male', 75000, 'Finance', 7),
(9, 'Aarti Desai', 'Female', 50000, 'IT', 3);
SELECT * FROM Employee;
Output:
Example 1 : Using HAVING to Filter Aggregated Results
This employee table will help us understand the HAVING Clause. It contains
employee IDs, Name, Gender, department, and salary. To Know the sum of salaries,
we will write the query:
Query:
SELECT Department, sum(Salary) as Salary
FROM Employee
GROUP BY department;
Output:
if we need to display the departments where the sum of salaries is 50,000 or more. In
this condition, we will use the HAVING Clause.
SELECT Department, sum(Salary) as Salary
FROM Employee
GROUP BY department
HAVING SUM(Salary) >= 50000;
Output:
Example 2: Using HAVING with Multiple Conditions
If we want to find the departments where the total salary is greater than or equal to
$50,000, and the average salary is greater than $55,000. We can use the HAVING
clause to apply both conditions.
Query
SELECT Department, SUM(Salary) AS Total_Salary, AVG(Salary) AS
Average_Salary
FROM Employee
GROUP BY Department
HAVING SUM(Salary) >= 50000 AND AVG(Salary) > 55000;
Output:
Departmen Total_Salary Average_Salar
t y
Finance 75000 75000
Sales 65000 65000
Example 3: Using HAVING with COUNT()
If we want to find departments where there are more than two employees. For this, we
can use the COUNT() aggregate function along with the HAVING clause.
Query:
SELECT Department, COUNT(EmployeeId) AS Employee_Count
FROM Employee
GROUP BY Department
HAVING COUNT(EmployeeId) >= 2;
Output:
Departmen Employee_Coun
t t
IT 2
This query counts the number of employees in each department and uses the HAVING
clause to filter for departments with more than two employees.
Example 4: Using HAVING with AVG()
In this example, let’s find out the average salary for each department and use the
HAVING clause to display only those departments where the average salary is greater
than $50,000.
Query:
SELECT Department, AVG(Salary) AS Average_Salary
FROM Employee
GROUP BY Department
HAVING AVG(Salary) > 50000;
Output:
Departmen Average_Salar
t y
Finance 75000
Marketing 55000
Sales 65000
Having vs WHERE
Having Where
In the HAVING clause it will check In the WHERE condition it will check or
the condition in group of a row. execute at each row individual.
Having Where
HAVING clause can only be used with The WHERE Clause cannot be used with
aggregate function. aggregate function like Having
Priority Wise HAVING Clause is Priority Wise WHERE is executed before
executed after Group By. Group By.
ORDER BY
The ORDER BY clause in SQL is a powerful feature used to sort query results in
either ascending or descending order based on one or more columns. Whether you’re
presenting data to users or analyzing large datasets, sorting the results in a structured
way is essential.
The ORDER BY statement in SQL is used to sort the fetched data in either ascending
or descending according to one or more columns. It is very useful to present data in a
structured manner.
SQL ORDER BY default mode is sorting data into ascending order. To sort data in
descending order use the DESC keyword with ORDER BY clause.
Syntax:
The syntax to use ORDER BY clause in SQL is:
SELECT * FROM table_name ORDER BY column_name ASC | DESC
Key Terms:
table_name: name of the table.
column_name: name of the column according to which the data is needed to be
arranged.
ASC: to sort the data in ascending order.
DESC: to sort the data in descending order.
SQL ORDER BY Clause Examples
We have created an Student table that stores Student data including their roll_no,
name, age, addess, and phone. Let’s look at some examples of the SQL ORDER BY
clause to understand it’s working in SQL.
We will use the following table in examples.
Example 1 : Sort According To a Single Column using ORDER BY Clause
In this example, we will fetch all data from the table Student and sort the result in
descending order according to the column ROLL_NO.
Query:
SELECT * FROM students ORDER BY ROLL_NO DESC;
Output:
Example 2 : Sort According To Multiple Columns using ORDER BY Clause
In this example, we will fetch all data from the table Student and then sort the result in
descending order first according to the column age. and then in ascending order
according to the column name. To sort according to multiple columns, separate the
names of columns by the (,) operator.
Query:
SELECT * FROM students ORDER BY age DESC , name ASC;
Output:
Sorting By Column Number
An integer that identifies the number of the column in the SelectItems in the
underlying query of the SELECT statement. Column number must be greater than 0
and not greater than the number of columns in the result table. In other words, if we
want to order by a column, that column must be specified in the SELECT list.
The rule checks for ORDER BY clauses that reference select list columns using the
column number instead of the column name. The column numbers in the ORDER BY
clause impair the readability of the SQL statement. Further, changing the order of
columns in the SELECT list has no impact on the ORDER BY when the columns are
referred to by names instead of numbers.
Syntax:
The Syntax to use ORDER BY Clause with Column Number
ORDER BY Column_Number asc/desc
Example of Sorting By Column Number
Here we take an example to sort a database table according to column 1 i.e Roll
Number. For this a query will be:
Query:
CREATE TABLE studentinfo
( Roll_no INT,
NAME VARCHAR(25),
Address VARCHAR(20),
CONTACTNO BIGINT NOT NULL,
Age INT );
INSERT INTO studentinfo
VALUES (7,'ROHIT','GHAZIABAD',9193458625,18),
(4,'DEEP','RAMNAGAR',9193458546,18),
(1,'HARSH','DELHI',9193342625,18),
(8,'NIRAJ','ALIPUR',9193678625,19),
(5,'SAPTARHI','KOLKATA',9193789625,19),
(2,'PRATIK','BIHAR',9193457825,19),
(6,'DHANRAJ','BARABAJAR',9193358625,20),
(3,'RIYANKA','SILIGURI',9193218625,20);
SELECT Roll_no, Name, Address
FROM studentinfo
ORDER BY 1
Output:
Important Points About ORDER BY Clause in SQL
The ORDER BY clause in SQL is used to sort the result set of a SELECT statement
based on specified columns.
It is essential for organizing query results and presenting data in a structured manner.
It can sort data in either ascending (ASC) or descending (DESC) order.
Multiple columns can be specified for sorting, allowing for more complex sorting
criteria.
We can use ORDER BY with WHERE clause, GROUP BY clause, and HAVING
clause.
GROUP BY
The GROUP BY statement in SQL is used for organizing and summarizing data based
on identical values in specified columns. By using the GROUP BY clause, users can
apply aggregate functions like SUM, COUNT, AVG, MIN, and MAX to each group,
making it easier to perform detailed data analysis.
In this article, we will learn the SQL GROUP BY syntax, explore practical examples
with single and multiple columns, and demonstrate advanced use cases with the
HAVING clause for conditional grouping. Whether you’re new to SQL or an
experienced professional, this article will help you master the GROUP BY clause for
efficient data querying.
GROUP BY Clause in SQL
The GROUP BY statement in SQL is used to arrange identical data into groups based
on specified columns. If a particular column has the same values in multiple rows, the
GROUP BY clause will group these rows together.
Key Points About GROUP BY:
GROUP BY clause is used with the SELECT statement.
In the query, the GROUP BY clause is placed after the WHERE clause.
In the query, the GROUP BY clause is placed before the ORDER BY clause if used.
In the query, the Group BY clause is placed before the Having clause.
Place condition in the having clause.
Syntax:
SELECT column1, function_name(column2)
FROM table_name
GROUP BY column1, column2
Key Terms
function_name: Name of the function used for example, SUM() , AVG().
table_name: Name of the table.
condition: Condition used.
Examples of GROUP BY in SQL
Let’s assume that we have two tables Employee and Student Sample Table is as
follows after adding two tables we will do some specific operations to learn about
GROUP BY.
Employee Table:
CREATE TABLE emp (
emp_no INT PRIMARY KEY,
name VARCHAR(50),
sal DECIMAL(10,2),
age INT
);
Insert some random data into a table and then we will perform some operations in
GROUP BY.
INSERT INTO emp (emp_no, name, sal, age) VALUES
(1, 'Aarav', 50000.00, 25),
(2, 'Aditi', 60000.50, 30),
(3, 'Aarav', 75000.75, 35),
(4, 'Anjali', 45000.25, 28),
(5, 'Chetan', 80000.00, 32),
(6, 'Divya', 65000.00, 27),
(7, 'Gaurav', 55000.50, 29),
(8, 'Divya', 72000.75, 31),
(9, 'Gaurav', 48000.25, 26),
(10, 'Divya', 83000.00, 33);
SELECT * from emp;
Output:
Student Table:
CREATE TABLE student (
name VARCHAR(50),
year INT,
subject VARCHAR(50)
);
INSERT INTO student (name, year, subject) VALUES
('Alice', 1, 'Mathematics'),
('Bob', 2, 'English'),
('Charlie', 3, 'Science'),
('David', 1, 'Mathematics'),
('Emily', 2, 'English'),
('Frank', 3, 'Science');
Output:
Operators in SQL
SQL operators are symbols or keywords used to perform operations on data in SQL
queries. These operations can include mathematical calculations, data comparisons,
logical manipulations, and more. Operators help you filter, calculate, and update data
in databases.
Types of Operators in SQL
SQL operators can be categorized based on the type of operation they perform. Here
are the primary types of SQL operators:
Arithmetic operator
Comparison operator
Logical operator
Bitwise Operators
Compound Operators
Each of these operators is essential for performing different types of operations on data
in SQL databases.
SQL Arithmetic Operators
Arithmetic operators in SQL are used to perform mathematical operations on
numeric values in queries. Some common arithmetic operators are:
Operator Description
+ The addition is used to perform an addition operation on the data values.
– This operator is used for the subtraction of the data values.
This operator works with the ‘ALL’ keyword and it calculates division
/
operations.
Operator Description
* This operator is used for multiplying data values.
% Modulus is used to get the remainder when data is divided by another.
SQL Arithmetic Operators Example
In this example, we will retrieve all records from the “employee” table where the
“emp_city” column does not start with the letter ‘A’.
Query:
SELECT * FROM employee WHERE emp_city NOT LIKE 'A%';
Output:
SQL Comparison Operators
Comparison Operators in SQL are used to compare one expression’s value to other
expressions. SQL supports different types of comparison operator, which are
described below:
Operator Description
= Equal to.
> Greater than.
< Less than.
>= Greater than equal to.
<= Less than equal to.
<> Not equal to.
SQL Comparison Operators Example
In this example, we will retrieve all records from the “MATHS” table where the value
in the “MARKS” column is equal to 50.
Query:
SELECT * FROM MATHS WHERE MARKS=50;
Output:
SQL Logical Operators
Logical Operators in SQL are used to combine or manipulate conditions in SQL
queries to retrieve or manipulate data based on specified criteria..
Operator Description
Logical AND compares two Booleans as expressions and returns true
AND when both expressions are true.
Logical OR compares two Booleans as expressions and returns true when
OR one of the expressions is true.
Not takes a single Boolean as an argument and change its value from false
NOT to true or from true to false.
SQL Logical Operators Example
In this example, retrieve all records from the “employee” table where the “emp_city”
column is equal to ‘Allahabad’ and the “emp_country” column is equal to ‘India’.
SELECT * FROM employee WHERE emp_city =
'Allahabad' AND emp_country = 'India';
Output:
SQL Bitwise Operators
Bitwise operators in SQL are used to perform bitwise operations on binary values in
SQL queries, manipulating individual bits to perform logical operations at the bit level.
Some SQL Bitwise Operators are:
Operator Description
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise XOR (exclusive OR) operator
~ Bitwise NOT (complement) operator
<< Left shift operator
>> Right shift operator
SQL Compound Operators
Compound operator in SQL are used to perform an operation and assign the result to
the original value in a single line. Some Compound operators are:
Operator Description
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
Operator Description
%= Modulo and assign
&= Bitwise AND and
assign
^= Bitwise XOR and assign
|= Bitwise OR and assign
SQL Special Operators
Special operators are used in SQL queries to perform specific operations like
comparing values, checking for existence, and filtering data based on certain
conditions.
Operators Description
ALL is used to select all records of a SELECT STATEMENT.
It compares a value to every value in a list of results from a
ALL query. The ALL must be preceded by the comparison operators
and evaluated to TRUE if the query returns no rows.
ANY compares a value to each value in a list of results from a
ANY query and evaluates to true if the result of an inner query
contains at least one row.
The SQL BETWEEN operator tests an expression against a
BETWEEN range. The range consists of a beginning, followed by an AND
keyword and an end expression.
The IN operator checks a value within a set of values separated
IN by commas and retrieves the rows from the table that match.
The EXISTS checks the existence of a result of a subquery. The
EXISTS subquery tests whether a subquery fetches at least one
EXISTS row. When no data is returned then this operator returns
‘FALSE’.
Operators Description
SOME operator evaluates the condition between the outer and
SOME inner tables and evaluates to true if the final result returns any
one row. If not, then it evaluates to false.
The UNIQUE operator searches every unique row of a specified
UNIQUE
table.
SQL Special Operator Example
In this example, we will retrieve all records from the “employee” table where the
“emp_id” column has a value that falls within the range of 101 to 104 (inclusive).
SELECT * FROM employee WHERE emp_id BETWEEN 101 AND 104;
Output:
SQL window functions are functions that perform calculations across a set of rows,
which are termed as a 'window'. A window function can access more than just the
current row of the query result. By framing a 'window' of rows onto the result set, you
can work with data from additional rows, which can result in more dynamic and
powerful queries.
SELECT ..., window_function(arg1, arg2, ...) OVER ([PARTITION BY partition_expression] [ORDER BY
sort_expression [ASC | DESC] ...]) ... FROM ...
Each window function includes an OVER clause that defines a window of rows around
the row being evaluated. The PARTITION BY clause divides the result set into
partitions (or groups), and the window function is applied to each partition separately.
The ORDER BY clause orders rows within a partition.
ROW_NUMBER Function
The ROW_NUMBER function is the simplest of all window functions. It assigns a
unique row number to each row within a partition, which starts from 1. When the data
changes, the row number changes.
SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) as row_number FROM employees
In the above query, the ROW_NUMBER() function is applied on the salary column in
descending order, meaning the employee with the highest salary will have a row
number of 1, the next highest will be 2, and so on.
RANK and DENSE_RANK Functions
The RANK and DENSE_RANK functions are similar to ROW_NUMBER function
but handle ties (rows with equal values) differently. When there is a tie, RANK
function leaves a gap in the sequence for the tied rows, whereas DENSE_RANK
function doesn't leave any gaps.
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) as rank, DENSE_RANK() OVER (ORDER
BY salary DESC) as dense_rank FROM employees
In the above query, both RANK() and DENSE_RANK() functions are applied to the
salary column. If two employees have the same salary, they will receive the same rank,
but the next rank will be skipped in RANK() while it won't be in DENSE_RANK().
LEAD and LAG Functions
The LEAD and LAG functions are quite unique as they allow access to a row at a
given physical offset that follows or precedes the current row.
LEAD function accesses data from a subsequent row without using a self-join.
Conversely, LAG function fetches data from a previous row.
SELECT name, salary, LAG(salary) OVER (ORDER BY salary DESC) as prev_salary, LEAD(salary) OVER
(ORDER BY salary DESC) as next_salary FROM employees
In the above query, LAG(salary) gives the salary of the employee who earns less than
the current employee, and LEAD(salary) gives the salary of the employee who earns
more than the current employee.
SELF Join
A self-join is a specific type of join in SQL where a table is joined with itself. In other
words, you treat a single table as if it were two separate tables, and then you perform a
join operation between these “virtual” tables within the same table.
Self-joins can be particularly useful in scenarios like:
Hierarchical Data: For example, when you have an organizational chart with
employees and their managers in the same table, and you want to retrieve information
about employees and their respective managers.
Recursive Data: When dealing with recursive data structures like comments on a post,
where comments can have replies (which are also comments), a self-join can help
retrieve the hierarchical structure.
Comparing Data within the Same Table: When you need to compare values within the
same table, such as finding employees with salaries greater than their managers’
salaries.
Find employees with salary more than their mangers salary
CREATE TABLE emp_manager (
emp_id INT,
emp_name VARCHAR(50),
salary INT,
manager_id INT
);
INSERT INTO emp_manager VALUES
(1, 'Ankit', 10000, 4),
(2, 'Mohit', 15000, 5),
(3, 'Vikas', 10000, 4),
(4, 'Rohit', 5000, 2),
(5, 'Mudit', 12000, 6),
(6, 'Agam', 12000, 2),
(7, 'Sanjay', 9000, 2),
(8, 'Ashish', 5000, 2);
This code creates the emp_manager table with the specified columns and inserts eight
rows of sample data into it. The data represents employees, their names, salaries, and
the manager_id that associates them with their respective managers.
SELECT e.emp_id, e.emp_name, [Link]
FROM emp_manager e
INNER JOIN emp_manager m ON e.manager_id = m.emp_id
WHERE [Link] > [Link];
We perform a self-join on the emp_manager table, using aliases e for the employees
and m for their managers.
We establish the join condition with e.manager_id = m.emp_id, which links employees
to their respective managers.
The WHERE clause filters the results to include only rows where an employee's salary
([Link]) is greater than their manager's salary ([Link]).
CROSS Join
A cross join, also known as a Cartesian join, is a type of join operation in SQL where
each row from one table is combined with every row from another table.
In essence, it forms the Cartesian product of the two tables, resulting in a large number
of rows equal to the product of the number of rows in each table involved in the join.
Unlike other types of joins (e.g., INNER JOIN, LEFT JOIN), there is no specific
condition specified for matching rows in a cross join. Instead, it simply combines all
possible combinations of rows.
Here are some scenarios where cross joins can be useful:
Creating Combinations: Cross joins are often used to create combinations of data. For
example, if you have a table of colors and a table of sizes, you can cross join them to
generate all possible combinations of colors and sizes.
Generating Test Data: Cross joins can be helpful for generating test data or sample
data for testing purposes, especially when you need a large dataset with various
combinations.
Creating Pivot Tables: In some cases, you might need to create pivot tables or matrices
where you want to combine rows and columns to form a matrix of data.
CREATE TABLE products (
id INT,
name VARCHAR(10)
);
INSERT INTO products VALUES
(1, 'A'),
(2, 'B'),
(3, 'C'),
(4, 'D'),
(5, 'E');
CREATE TABLE colors (
color_id INT,
color VARCHAR(50)
);
INSERT INTO colors VALUES
(1, 'Blue'),
(2, 'Green'),
(3, 'Orange');
CREATE TABLE sizes (
size_id INT,
size VARCHAR(10)
);
INSERT INTO sizes VALUES
(1, 'M'),
(2, 'L'),
(3, 'XL');
CREATE TABLE transactions (
order_id INT,
product_name VARCHAR(20),
color VARCHAR(10),
size VARCHAR(10),
amount INT
);
INSERT INTO transactions VALUES
(1, 'A', 'Blue', 'L', 300),
(2, 'B', 'Blue', 'XL', 150),
(3, 'B', 'Green', 'L', 250),
(4, 'C', 'Blue', 'L', 250),
(5, 'E', 'Green', 'L', 270),
(6, 'D', 'Orange', 'L', 200),
(7, 'D', 'Green', 'M', 250);
SELECT
[Link] AS product_name,
[Link] AS product_color,
[Link] AS product_size
FROM
products p
CROSS JOIN
colors c
CROSS JOIN
sizes s;
The query starts by selecting specific columns from the products, colors, and sizes
tables and assigns aliases to those columns. Then, it uses CROSS JOIN operations to
generate all possible combinations of these columns from the respective tables.
The result will be a very large dataset containing every product name, color, and size
combination.
CTE
A Common Table Expression (CTE) is a temporary result set that is used within a
larger query to simplify and maintain complex queries:
Definition: A CTE is a named subquery defined within the scope of a single statement.
It is created from a simple query and can be referred to multiple times within the
statement.
Purpose: CTEs are used to break down complex queries into smaller, reusable blocks.
This improves readability and performance by reducing the complexity of the query.
Types: There are four types of CTEs: standard, sequential, recursive, and nested.
Availability: CTEs are available in most modern relational database management
systems (RDBMS), including MySQL, PostgreSQL, Oracle Database, SQL Server,
SQLite, IBM Db2, and MariaDB.
Example: The following code is an example of a query that uses a CTE:
Code
WITH my_cte (cte_col_1, cte_col_2) AS ( SELECT col_1, col_2 FROM ... )
SELECT ... FROM my_cte;