What Is SQL?
What Is SQL?
Structured Query Language (SQL) is the standard language used to interact with relational
databases.
• Allows users to store, retrieve, update, and manage data efficiently through simple
commands.
• Known for its user-friendly syntax and powerful capabilities, SQL is widely used across
industries.
• SQL Engine breaks down, optimizes and executes these queries efficiently
• DBMS tools like MySQL and SQL Server have their own SQL engine and an interface
where users can write and execute SQL queries.
Below are the detailed steps involved in the SQL query execution.
1. Input: The user submits a query (e.g., SELECT, INSERT, UPDATE, DELETE) via an
application or interface.
2. Parsing: The query processor breaks the query into parts (tokens) and checks for syntax
and schema correctness.
3. Optimization: The optimizer finds the most efficient way to run the query using indexes,
statistics, and available resources.
4. Execution: The execution engine runs the query using the chosen plan, accessing or
modifying the database as needed.
5. Output: Results are returned to the user — either data (for SELECT) or a success
message (for other operations).
• Tables: Each table enforces rules and relationships among its columns for data
integrity.
• Indexes: Indexes speed up queries by allowing the database to quickly locate data
without scanning the entire table.
• Views: A view is a virtual table—basically a saved SELECT statement you can query like
a table.
• Stored Procedures: These are pre-written SQL scripts stored inside the database. They
can receive inputs, run complex logic, and return results boosting performance,
reusability, and security.
• Security & Permissions: SQL includes tools to restrict access, letting DBAs assign
who can do what whether it's accessing tables, executing procedures, or changing
structures.
• Joins: Joins combine data from multiple tables based on relationships essential for
querying across related datasets.
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.
• End with Semicolon (;): Each SQL statement must end with a semicolon to execute
properly.
• Case Insensitivity: SQL keywords (e.g., SELECT, INSERT) are not case-sensitive.
However, table/column names may be case-sensitive depending on the DBMS.
• Whitespace Allowed: Queries can span multiple lines, but use spaces between
keywords and names.
• Reserved Words: Avoid using SQL keywords as names. If needed, wrap them in quotes
(" ") or backticks (`).
Comments
• Single-line: -- comment
• Multi-line: /* comment */
Data Constraints: Use NOT NULL, UNIQUE, PRIMARY KEY, etc., to ensure data
accuracy.
Naming Rules:
• Max 30 characters
Command Description
A relational database can be updated with new data using data manipulation language
(DML) statements. The INSERT command, for instance, is used by an application to add
a new record to the database.
Command Description
Data retrieval instructions are written in the data query language (DQL), which is used to
access relational databases. The SELECT command is used by software programs to
filter and return particular results from a SQL table.
Command Description
Benefits of SQL
• Efficiency: SQL is designed to handle complex queries and large datasets with optimal
performance, making data retrieval and manipulation seamless.
• Flexibility: SQL can be extended with procedural programming (e.g., PL/SQL, T-SQL) to
build complex business logic and custom functions.
Limitations of SQL
• Lack of Real Time Analytics: Traditional SQL databases are not optimized for real-time
data ingestion and analysis.
• Machine Learning and Data Science: Combine SQL databases with tools like Python,
R, and TensorFlow to streamline machine learning workflows.
•
In SQL, each column must be assigned a data type that defines the kind of data it can
store, such as integers, dates, text, or binary values. Choosing the correct data type is
crucial for data integrity, query performance and efficient indexing.
• Memory-efficient storage
Numeric data types are fundamental to database design and are used to store numbers,
whether they are integers, decimals or floating-point numbers. These data types allow
for mathematical operations like addition, subtraction, multiplication and division,
which makes them essential for managing financial, scientific and analytical data.
Exact numeric types are used when precise numeric values are needed, such as for
financial data, quantities, and counts. Some common exact numeric types include:
Data Type Description Range
Very small
TINYINT 0 to 255
integers
Exact fixed-point
numbers (e.g.,
DECIMAL -10^38 + 1 to 10^38 - 1
for financial
values)
Similar to
DECIMAL, used
NUMERIC -10^38 + 1 to 10^38 - 1
for precision
data
Smaller -214,748.3648 to
SMALLMONEY
monetary values 214,748.3647
These types are used to store approximate values, such as scientific measurements or
large ranges of data that don't need exact precision.
Data
Type Description Range
-1.79E+308 to
FLOAT Approximate numeric values
1.79E+308
Character data types are used to store text or character-based data. The choice
between fixed-length and variable-length data types depends on the nature of your data.
Unicode data types are used to store characters from any language, supporting a wider
variety of characters. These are given in below table.
SQL provides several data types for storing date and time information. They are essential
for managing timestamps, events and time-based queries. These are given in the below
table.
Storage
Data Type Description Size
Binary data types are used to store binary data such as images, videos or other file
types. These include:
SQL also supports some specialized data types for advanced use cases:
• XML Data Type: Used to store XML data and manipulate XML structures in the database
• Spatial Data Type (Geometry): stores planar spatial data, such as points, lines, and
polygons, in a database table.
SQL Operators
SQL operators are symbols or keywords used to perform operations on data in SQL
queries.
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
• Special Operators
Each of these operators is essential for performing different types of operations on data
in SQL databases.
Operator Description
Query:
Output:
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.
In this example, we will retrieve all records from the "MATHS" table where the value in
the "MARKS" column is equal to 50.
Query:
Output:
Comparison Operation
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
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'.
Query:
Output:
Logical Operation
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 OR operator
Operator Description
SQL also provides several special operators that serve specific functions such as
filtering data based on a range, checking for existence, and comparing sets of values.
Operators Description
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).
Query:
Output:
Special Operator
SQL commands are the fundamental building blocks for communicating with
a database management system (DBMS). 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.
DDL or Data Definition Language actually consists of the SQL commands that can be
used for 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
COMMENT
Add comments to the
COMMENT 'comment_text' ON TABLE
data dictionary
table_name;
RENAME TABLE
Rename an object
RENAME old_table_name TO
existing in the database
new_table_name;
Example:
In this example, a new table called employees is created with columns for employee ID,
first name, last name and hire date.
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
Command Description Syntax
Indicates
SELECT column1
FROM the table(s) from
FROM table_name;
which to retrieve data.
SELECT column1,
Groups rows that have
GROUP AGG_FUNCTION(column2)
the same values in
BY FROM table_name
specified columns.
GROUP BY column1;
SELECT column1,
AGG_FUNCTION(column2)
Filters the results
HAVING FROM table_name
of GROUP BY
GROUP BY column1
HAVING condition;
SELECT column1
ORDER Sorts the result set by FROM table_name
BY one or more columns ORDER BY column1 [ASC |
DESC];
By default, it sorts
in ascending SELECT * FROM table_name
LIMIT
order unless specified LIMIT number;
as DESC
Example:
This query retrieves employees' first and last names, along with their hire dates, from the
employees table, specifically for those in the 'Sales' department, sorted by hire date.
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.
Delete records
DELETE FROM table_name WHERE
DELETE from a
condition;
database table
Call a PL/SQL
CALL or JAVA CALL procedure_name(arguments);
subprogram
Describe the
EXPLAIN EXPLAIN PLAN FOR SELECT * FROM
access path to
PLAN table_name;
data
Example:
This query inserts a new record into the employees table with the first name 'Jane', last
name 'Smith' and department 'HR'.
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.
Example of DCL
This command grants the user user_name the permissions to select and update records
in the employees table.
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.
Creates a savepoint
SAVEPOINT
SAVEPOINT within the current
savepoint_name;
transaction
Example:
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;
In this example, a transaction is started, changes are made, and a savepoint is set. If
needed, the transaction can be rolled back to the savepoint before being committed.
There are also a few other SQL Commands we often rely on when writing powerful
queries. While they don’t fit neatly into the five main categories, they’re absolutely
essential for working with data effectively.
Command Description
IN / BETWEEN /
Used for advanced filtering conditions.
LIKE
Command Description
SQL databases or relational databases are widely used for storing, managing and
organizing structured data in a tabular format. These databases store data in tables
consisting of rows and columns. SQL is the standard programming language used to
interact with these databases. It enables users to create, update, retrieve and manage
data effectively.
• Efficient Data Handling: SQL databases are designed to store, retrieve and manipulate
structured data effectively.
• Reliability: They ensure data consistency and integrity, making them ideal for high
priority applications.
• Scalability: SQL databases efficiently scale to handle growing data volumes while
maintaining performance.
• ACID Compliance: SQL databases guarantee data accuracy and reliability through
ACID properties (Atomicity, Consistency, Isolation, Durability).
• Universal Adoption: As the standard for relational data, SQL databases are widely used
across industries, from business to government.
SQL databases work using a client-server system. The database server stores all the
data, while the client interacts with the server by sending queries to retrieve or update
the data.
• Data Storage: Data is stored in tables, each containing rows and columns. Each column
defines a specific attribute, and each row represents a unique entity or record.
• Query Processing: When a user submits a query, the database server processes it by
parsing the SQL command, optimizing the query for performance, and executing it to
retrieve or manipulate the data.
• Data Retrieval: SQL queries can retrieve specific data using SELECT statements and
filter results using WHERE clauses. The server returns the queried data in a structured
format for use by applications.
It explains the key processes involved in creating, selecting, renaming and dropping SQL
databases. Understanding these commands is essential for managing your databases
effectively.
1. CREATE Database
Example:
2. SELECT Database
The USE or SELECT DATABASE command allows you to select a specific database for the
current session. Once selected, the database becomes the active database, and all
subsequent queries will be executed on it. This command is essential when working
with multiple databases.
USE test_db;
3. RENAME Database
The RENAME DATABASE command can be used where you need to rename an existing
database. All databases does not support this command, it can be useful when we need
to update the name of a database to show its content.
4. DROP Database
The DROP DATABASE command is used to permanently delete a database, along with all
its tables, data and objects. This action is irreversible, so it should be used carefully. It is
helpful when you no longer need a database and want to free up system resources.
Below you will find the key differences between SQL and NoSQL databases:
Key-value pairs,
Data Tables with rows and
documents, columns,
Structure columns.
graphs.
Supports complex
Limited relationship
Relationships joins and
support.
relationships.
Conclusion
SQL databases are used for managing structured data in many applications. They
provide various benefits such as data consistency, optimized performance, scalibility
and minimum redundancy. SQL Databases are ideal for handling complex queries and
transactional systems.
Creating a table is one of the first and most important steps in building a database. The CREATE
TABLE command in SQL defines how your data will be stored, including the table name, column
names, data types, and rules (constraints) such as NOT NULL, PRIMARY KEY, and CHECK.
Syntax:
columnN datatype(size)
);
Key Terms
Let’s walk through a practical example where we create a Customer table that stores customer
data. We will define various columns such as CustomerID, CustomerName, Country, Age, and
Phone with appropriate data types and constraints.
Query:
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Phone int(10)
);
Output:
Explanation:
• CustomerID is an integer and serves as the PRIMARY KEY, ensuring each record is
unique.
• Age has a CHECK constraint, ensuring it’s within the range of 0 to 99.
• Phone is an integer field, although in real scenarios, a VARCHAR would often be used for
storing phone numbers to allow for leading zeros and formatting.
After creating the table, you can use INSERT INTO command to add data into it. Here is how to
add some sample records into the Customer table:
Output:
Note: If you are working with a large amount of data, consider using bulk inserts or importing
data from external files to optimize performance.
We can also create a new table based on the structure (and optionally the data) of an existing
table. The CREATE TABLE AS SELECT command allows us to duplicate an entire table or select
specific columns to form a new one. The following query creates a new table called SubTable
that contains CustomerID and CustomerName from the existing Customer table.
Syntax:
FROM existing_table_name
WHERE ...;
In this example, we create a new table SubTable that contains just the CustomerID and
CustomerName columns from the Customer table. This method is useful for creating backups
or performing quick data migrations.
Query:
FROM customer;
Output:
Note: We can use * instead of column name to copy whole table to another table.
To ensure the smooth creation and management of your tables, keep these points in mind:
1. The CREATE TABLE statement can also define constraints like NOT NULL, UNIQUE, and
DEFAULT.
2. If you attempt to create a table that already exists, SQL will throw an error. To avoid this, you
can use the IF NOT EXISTS clause.
3. Always define appropriate data types for each column (e.g., VARCHAR(50) for names
and INT for IDs) to optimize performance and storage.
4. After creating a table, use the following command to view the structure of your table:
DESC table_name;
5. If you need to change the table’s structure after creation (e.g., renaming a column, adding a
new column), use the ALTER TABLE statement.
SQL SELECT is used to retrieve data from one or more tables, either all records or specific
results based on conditions. It returns the output in a tabular format of rows and columns.
Syntax:
Parameters
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
Output:
In this example, we will demonstrate how to retrieve specific columns from the Customer table.
Here we will fetch only CustomerName and LastName for each record.
Query:
Output
In this example, we will fetch all the fields from the table Customer:
Query:
Output
Suppose we want to see table values with specific conditions then WHERE Clause is used with
select statement. In this example, filter customers who are 21 years old.
Query:
SELECT CustomerName
FROM Customer
In this example, we will use SELECT statement with GROUP BY Clause to Group rows and
perform aggregation. Here, Count orders per customer.
Query:
FROM Orders
GROUP BY Customer_id;
Output:
Use HAVING to filter results after grouping. Consider the following database for fetching
departments with total salary above 50,000. Use WHERE for row-level filtering, HAVING for
group-level filtering.
Query:
FROM employee
GROUP BY department
Output:
Example 6: SELECT Statement with ORDER BY clause in SQL
In this example, we will use SELECT Statement with ORDER BY clause. Here, Sort results
by Age in descending order.
Query:
Output:
Join tables SELECT a.*, b.* FROM TableA a JOIN TableB b ON a.id = b.id;
The INSERT INTO statement in SQL is used to add new rows to an existing table, whether for all
columns, specific columns or by copying from another table. It is an essential command for
populating databases with relevant records like customers, employees, or students.
This method is used when you want to insert data into all columns of a table without specifying
column names. We simply provide the values for each column, in the same order that the
columns are defined in the table.
Syntax:
Parameters:
• table_name: The name of the table where the data will be inserted
• value1, value2: The values you want to insert into the respective columns of the table
Example: For better understanding, let's look at the SQL INSERT INTO statement with
examples. Let us first create a table named 'Student'.
USE StudentDB;
CREATE TABLE Student (
NAME VARCHAR(50),
ADDRESS VARCHAR(100),
PHONE VARCHAR(15),
AGE INT
);
Output
If we don’t want to specify the column names (and you’re inserting data into all columns), we
can directly insert values in the order they appear in the table structure. Here's an example:
Query:
INSERT INTO Student
Output
In some cases, you might want to insert data into only certain columns, leaving the others
empty or with default values. In such cases, we can specify the column names explicitly.
Syntax
Parameters:
• value1, value2, value..: the values for each specified column of the new record.
Example: Let’s say we only want to insert the student's ID, name, and age into
the Students table, and leave the address and phone number as NULL (the default value).
Output:
Note: Columns not included in the INSERT statement are filled with default values (typically
NULL).
Instead of running multiple INSERT INTO commands, you can insert multiple rows into a table in
a single query. This is more efficient and reduces the number of database operations.
Syntax
VALUES
Example: If we want to add multiple students to the Students table in one go, the query would
look like this:
VALUES
(6, 'Amit Kumar', 15, 'Delhi', 'XXXXXXXXXX'),
Output:
Explanation:
• This method is faster than running multiple individual INSERT INTO commands.
• If you're inserting more than 1000 rows, consider using bulk insert or multiple insert
statements for efficiency.
We can also copy data from one table into another table using the INSERT INTO
SELECT statement. This is very useful when we want to move or replicate data from one table to
another without manually typing all the data.
Method 1: Insert All Columns from Another Table
Example: If you want to copy all data from the OldStudents table into the Students table, use
this query:
FROM source_table;
Example: Let’s say we want to copy only the Name and Age columns
from OldStudents into Students:
FROM OldStudents;
You can also insert specific rows based on a condition by using the WHERE clause with
the SELECT statement.
Example: If we want to copy only students older than 20 years from OldStudents to Students,
we would write:
Best Practices
Point Description
NULL Values Missing columns will be set to NULL or their default values
Insert from
Easily copy records between tables with SELECT
SELECT
The UPDATE statement in SQL is used to modify existing records in a table without deleting
them. It allows updating one or multiple columns, with or without conditions, to keep data
accurate and consistent.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE condition;
Parameters
• SET: The column(s) you want to update and their new values.
Note: The SET keyword assigns new values to columns, while the WHERE clause selects which
rows to update. Without WHERE, all rows will be updated.
Examples of SQL UPDATE Statement
Let’s begin by creating a Customer table with some sample data. This table contains each
customer's unique ID, name, last name, phone number and country. We will use it to
demonstrate how the UPDATE statement works in SQL.
Query:
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
Output
Query:
UPDATE Customer
Output:
Explanation: Only the rows where Age is 22 will be updated, and the CustomerName will be set
to 'Nitin'.
We need to update both the CustomerName and Country for a specific CustomerID.
Query:
UPDATE Customer
Country = 'USA'
WHERE CustomerID = 1;
Output:
Explanation: For the row where CustomerID is 1, both CustomerName and Country will be
updated simultaneously.
Note: For updating multiple columns we have used comma(,) to separate the names and values
of two columns.
If we accidentally omit the WHERE clause, all the rows in the table will be updated, which is a
common mistake. Let’s update the CustomerName for every record in the table:
Query:
UPDATE Customer
Output
Explanation: This will set the CustomerName for every row in the Customer table to
'Shubham'. Be careful while omitting the WHERE clause, as this action is irreversible unless you
have a backup.
• Avoid frequent updates: Constantly updating rows can slow down performance. Batch
updates or consider using a database trigger to handle automatic updates.
• Index relevant columns: Ensure that columns in the WHERE clause (such
as CustomerID) are indexed. This will improve the speed of the update operation.
The most important point when using the UPDATE statement is to always include
a WHERE clause unless you genuinely intend to update all rows.
Run a SELECT query to view the data you want to update before executing
the UPDATE statement. This helps avoid accidental data modifications.
BEGIN TRANSACTION;
4. Test on a small dataset first: If you are uncertain about the impact of your UPDATE, test it on
a small subset of data to ensure the changes are as expected.
The SQL DELETE statement is used to remove specific rows from a table while keeping the table
structure intact. It is different from DROP, which deletes the entire table.
Syntax:
Parameter Explanation
• table_name: The name of the table from which you want to delete the rows
Note: We can delete single as well as multiple records depending on the condition we provide in
the WHERE clause. If we omit the WHERE clause then all of the records will be deleted and the
table will be empty.
Examples of SQL DELETE Statement
Assume we have created a table named GFG_Employee in SQL, which contains the personal
details of the Employee including their id, name, email and department etc. as shown below.
department VARCHAR(20)
);
Output
Query:
Output:
To delete multiple records, you can specify a condition that matches several rows. Let's delete
the rows from the table GFG_Employees where the department is "Development". This will
delete 2 rows (the first row and the seventh row).
Query
Output
output
If we need to delete all records from the table, we can omit the WHERE clause, or alternatively
use the DELETE statement with an asterisk (*) to denote all rows.
Query:
Or
Output:
output
All of the records in the table will be deleted, there are no records left to display. The table
GFG_Employees will become empty.
Since the DELETE statement is a DML operation, it can be rolled back when executed in a
statement. If you accidentally delete records or need to repeat the process, you can use
the ROLLBACK command.
Query:
START TRANSACTION;
ROLLBACK;
Explanation: The ROLLBACK command will undo the changes made by the DELETE statement,
effectively restoring the records that were deleted during the transaction.
In SQL, the WHERE clause is used to filter rows based on specific conditions. Whether you are
retrieving, updating, or deleting data, WHERE ensures that only relevant records are affected.
Without it, your query applies to every row in the table! The WHERE clause helps you:
Syntax:
FROM table_name
Parameters:
We will create a basic employee table structure in SQL for performing all the where clause
operation.
Query:
Output:
Query:
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:
The BETWEEN operator is used to filter records within a specified range, and it includes both
the start and end values. In this example, we want to find employees whose age is between 22
and 24, including both 22 and 24.
Query:
Output:
It is used to fetch filtered data by searching for a particular pattern in the where clause. In this
example we want to find records of Employees where Name starts with the letter. The
'%'(wildcard) signifies the later characters here which can be of any length and value.
Query:
Output:
Query:
Output:
Example 5: 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. Here we want to find the
Names of Employees where Age is 21 or 23.
Query:
Output:
Operator Description
= Equal to
SQL | Aliases
ead and write. They don’t change the actual names in the database and exist only for the
duration of that query.
We will use the following Customer table to demonstrate all SQL alias concepts. This table
contains customer information such as ID, name, country, age, and phone number.
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age INT,
Phone VARCHAR(15)
);
VALUES
Output:
1. Column Aliases
A column alias is used to rename a column just for the output of a query. They are useful when:
• Performing calculations
Syntax:
FROM table_name;
• alias_name: temporary name that we are going to assign for the column or table.
• AS: It is optional. If you have not specified it, there is no effect on the query execution.
Example 1: Column Alias for Renaming a Column
SELECT CustomerID AS id
FROM Customer;
Output:
id
2. Table Aliases
A table alias is used when you want to give a table a temporary name for the duration of a query.
Table aliases are especially helpful in JOIN operations to simplify queries, particularly when the
same table is referenced multiple times (like in self-joins).
We want to join the Customer table with itself to find customers who have the same country
and are aged 21. We will use table aliases for each instance of the Customer table.
Query:
Output:
CustomerName Country
Shubham India
CustomerName Country
Aman Australia
Aditya Austria
Nishant Spain
Here, c1 and c2 are aliases for two instances of the Customer table.
We want to fetch customers who are aged 21 or older and rename the columns for better clarity.
We will use both table and column aliases.
Query:
FROM Customer AS c
Output:
Name Location
Shubham India
Aman Australia
Aditya Austria
Nishant Spain
SQL joins are fundamental tools for combining data from multiple tables in relational
databases.
• For example, consider two tables where one table (say Student) has student information
with id as a key and other table (say Marks) has information about marks of every
student id. Now to display the marks of every student with name, we need to join the two
tables.
The INNER JOIN keyword selects all rows from both the tables as long as the condition is
satisfied. This keyword will create the result set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be the same.
Syntax:
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
Inner Join
Consider the two tables, Student and StudentCourse, which share a common
column ROLL_NO. Using SQL JOINS, we can combine data from these tables based on their
relationship, allowing us to retrieve meaningful information like student details along with their
enrolled courses.
1. Student Table:
Student
2. StudentCourse Table:
StudentCourse
Let's look at the example of INNER JOIN clause, and understand it's working. This query will
show the names and age of students enrolled in different courses.
Query:
Output
Output
A LEFT JOIN returns all rows from the left table, along with matching rows from the right table. If
there is no match, NULL values are returned for columns from the right table. LEFT JOIN is also
known as LEFT OUTER JOIN.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are the same.
Left JOIN
In this example, the LEFT JOIN retrieves all rows from the Student table and the matching rows
from the StudentCourse table based on the ROLL_NO column.
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output
Output
RIGHT JOIN returns all the rows of the table on the right side of the join and matching rows for
the table on the left side of the join. It is very similar to LEFT JOIN for the rows for which there is
no matching row on the left side, the result-set will contain null. RIGHT JOIN is also known as
RIGHT OUTER JOIN.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Key Terms
Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are the same
Right JOIN
In this example, the RIGHT JOIN retrieves all rows from the StudentCourse table and the
matching rows from the Student table based on the ROLL_NO column.
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output
Output
FULL JOIN creates the result-set by combining results of both LEFT JOIN and RIGHT JOIN. The
result-set will contain all the rows from both tables. For the rows for which there is no matching,
the result-set will contain NULL values.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Key Terms
• table1: First table.
This example demonstrates the use of a FULL JOIN, which combines the results of both LEFT
JOIN and RIGHT JOIN. The query retrieves all rows from the Student and StudentCourse tables.
If a record in one table does not have a matching record in the other table, the result set will
include that record with NULL values for the missing fields
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output
NAME COURSE_ID
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NAME COURSE_ID
NULL 4
NULL 5
NULL 4
A Natural Join is a type of INNER JOIN that automatically joins two tables based on columns
with the same name and data type. It returns only the rows where the values in the common
columns match.
• It returns rows where the values in these common columns are the same in both tables.
• Common columns appear only once in the result, even if they exist in both tables.
• Unlike a CROSS JOIN, which creates all possible combinations of rows, a Natural Join
only includes rows with matching values
Example:
Employee
1 Ram 10
2 Jon 30
3 Bob 50
Department
Dept_id Dept_name
Department
10 IT
30 HR
40 TIS
(Employee) ? (Department)
Output:
1 Ram 10 10 IT
2 Jon 30 30 HR
The UPDATE statement in SQL is used to modify existing records in a table without deleting
them. It allows updating one or multiple columns, with or without conditions, to keep data
accurate and consistent.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE condition;
Parameters
Note: The SET keyword assigns new values to columns, while the WHERE clause selects which
rows to update. Without WHERE, all rows will be updated.
Let’s begin by creating a Customer table with some sample data. This table contains each
customer's unique ID, name, last name, phone number and country. We will use it to
demonstrate how the UPDATE statement works in SQL.
Query:
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
Output
Example 1: Update Single Column Using UPDATE Statement
We have a Customer table and we want to Update the CustomerName where the Age is 22.
Query:
UPDATE Customer
Output:
Explanation: Only the rows where Age is 22 will be updated, and the CustomerName will be set
to 'Nitin'.
We need to update both the CustomerName and Country for a specific CustomerID.
Query:
UPDATE Customer
Country = 'USA'
WHERE CustomerID = 1;
Output:
Explanation: For the row where CustomerID is 1, both CustomerName and Country will be
updated simultaneously.
Note: For updating multiple columns we have used comma(,) to separate the names and values
of two columns.
If we accidentally omit the WHERE clause, all the rows in the table will be updated, which is a
common mistake. Let’s update the CustomerName for every record in the table:
Query:
UPDATE Customer
Output
Explanation: This will set the CustomerName for every row in the Customer table to
'Shubham'. Be careful while omitting the WHERE clause, as this action is irreversible unless you
have a backup.
• Index relevant columns: Ensure that columns in the WHERE clause (such
as CustomerID) are indexed. This will improve the speed of the update operation.
The most important point when using the UPDATE statement is to always include
a WHERE clause unless you genuinely intend to update all rows.
Run a SELECT query to view the data you want to update before executing
the UPDATE statement. This helps avoid accidental data modifications.
BEGIN TRANSACTION;
4. Test on a small dataset first: If you are uncertain about the impact of your UPDATE, test it on
a small subset of data to ensure the changes are as expected.
The SQL DELETE statement is used to remove specific rows from a table while keeping the table
structure intact. It is different from DROP, which deletes the entire table.
Syntax:
Parameter Explanation
• table_name: The name of the table from which you want to delete the rows
Note: We can delete single as well as multiple records depending on the condition we provide in
the WHERE clause. If we omit the WHERE clause then all of the records will be deleted and the
table will be empty.
Assume we have created a table named GFG_Employee in SQL, which contains the personal
details of the Employee including their id, name, email and department etc. as shown below.
department VARCHAR(20)
);
Output
Example 1: Deleting Single Record
We can use the DELETE statement with a condition to delete a specific row from a table.
The WHERE clause ensures only the intended record is removed. We can delete the records
named Rithvik by using the below query:
Query:
Output:
Query
Output
output
If we need to delete all records from the table, we can omit the WHERE clause, or alternatively
use the DELETE statement with an asterisk (*) to denote all rows.
Query:
Or
Output:
output
All of the records in the table will be deleted, there are no records left to display. The table
GFG_Employees will become empty.
Since the DELETE statement is a DML operation, it can be rolled back when executed in a
statement. If you accidentally delete records or need to repeat the process, you can use
the ROLLBACK command.
Query:
START TRANSACTION;
Explanation: The ROLLBACK command will undo the changes made by the DELETE statement,
effectively restoring the records that were deleted during the transaction.
The SQL DELETE statement is used to remove specific rows from a table while keeping the table
structure intact. It is different from DROP, which deletes the entire table.
Syntax:
Parameter Explanation
• table_name: The name of the table from which you want to delete the rows
Note: We can delete single as well as multiple records depending on the condition we provide in
the WHERE clause. If we omit the WHERE clause then all of the records will be deleted and the
table will be empty.
Assume we have created a table named GFG_Employee in SQL, which contains the personal
details of the Employee including their id, name, email and department etc. as shown below.
department VARCHAR(20)
);
Output
We can use the DELETE statement with a condition to delete a specific row from a table.
The WHERE clause ensures only the intended record is removed. We can delete the records
named Rithvik by using the below query:
Query:
Output:
Example 2: Deleting Multiple Records
To delete multiple records, you can specify a condition that matches several rows. Let's delete
the rows from the table GFG_Employees where the department is "Development". This will
delete 2 rows (the first row and the seventh row).
Query
Output
output
If we need to delete all records from the table, we can omit the WHERE clause, or alternatively
use the DELETE statement with an asterisk (*) to denote all rows.
Query:
Or
Output:
output
All of the records in the table will be deleted, there are no records left to display. The table
GFG_Employees will become empty.
Since the DELETE statement is a DML operation, it can be rolled back when executed in a
statement. If you accidentally delete records or need to repeat the process, you can use
the ROLLBACK command.
Query:
START TRANSACTION;
ROLLBACK;
Explanation: The ROLLBACK command will undo the changes made by the DELETE statement,
effectively restoring the records that were deleted during the transaction.
In SQL, the WHERE clause is used to filter rows based on specific conditions. Whether you are
retrieving, updating, or deleting data, WHERE ensures that only relevant records are affected.
Without it, your query applies to every row in the table! The WHERE clause helps you:
• Filter rows that meet certain conditions
Syntax:
FROM table_name
Parameters:
We will create a basic employee table structure in SQL for performing all the where clause
operation.
Query:
Query:
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:
Query:
Output:
It is used to fetch filtered data by searching for a particular pattern in the where clause. In this
example we want to find records of Employees where Name starts with the letter. The
'%'(wildcard) signifies the later characters here which can be of any length and value.
Query:
Output:
Query:
Output:
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. Here we want to find the
Names of Employees where Age is 21 or 23.
Query:
Operator Description
= Equal to
SQL | Aliases
In SQL, aliases are temporary names given to columns or tables to make queries easier to read
and write. They don’t change the actual names in the database and exist only for the duration of
that query.
• Make long or complex names readable
We will use the following Customer table to demonstrate all SQL alias concepts. This table
contains customer information such as ID, name, country, age, and phone number.
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age INT,
Phone VARCHAR(15)
);
VALUES
Output:
CustomerID CustomerName LastName Country Age Phone
1. Column Aliases
A column alias is used to rename a column just for the output of a query. They are useful when:
• Performing calculations
Syntax:
FROM table_name;
• alias_name: temporary name that we are going to assign for the column or table.
• AS: It is optional. If you have not specified it, there is no effect on the query execution.
SELECT CustomerID AS id
FROM Customer;
Output:
id
2. Table Aliases
A table alias is used when you want to give a table a temporary name for the duration of a query.
Table aliases are especially helpful in JOIN operations to simplify queries, particularly when the
same table is referenced multiple times (like in self-joins).
We want to join the Customer table with itself to find customers who have the same country
and are aged 21. We will use table aliases for each instance of the Customer table.
Query:
Output:
CustomerName Country
Shubham India
Aman Australia
Aditya Austria
Nishant Spain
Here, c1 and c2 are aliases for two instances of the Customer table.
We want to fetch customers who are aged 21 or older and rename the columns for better clarity.
We will use both table and column aliases.
Query:
FROM Customer AS c
Output:
Name Location
Shubham India
Aman Australia
Aditya Austria
Nishant Spain
SQL joins are fundamental tools for combining data from multiple tables in relational
databases.
• For example, consider two tables where one table (say Student) has student information
with id as a key and other table (say Marks) has information about marks of every
student id. Now to display the marks of every student with name, we need to join the two
tables.
The INNER JOIN keyword selects all rows from both the tables as long as the condition is
satisfied. This keyword will create the result set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be the same.
Syntax:
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
Inner Join
Consider the two tables, Student and StudentCourse, which share a common
column ROLL_NO. Using SQL JOINS, we can combine data from these tables based on their
relationship, allowing us to retrieve meaningful information like student details along with their
enrolled courses.
1. Student Table:
Student
2. StudentCourse Table:
StudentCourse
Let's look at the example of INNER JOIN clause, and understand it's working. This query will
show the names and age of students enrolled in different courses.
Query:
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
Output
Output
A LEFT JOIN returns all rows from the left table, along with matching rows from the right table. If
there is no match, NULL values are returned for columns from the right table. LEFT JOIN is also
known as LEFT OUTER JOIN.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are the same.
Left JOIN
LEFT JOIN Example
In this example, the LEFT JOIN retrieves all rows from the Student table and the matching rows
from the StudentCourse table based on the ROLL_NO column.
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output
Output
RIGHT JOIN returns all the rows of the table on the right side of the join and matching rows for
the table on the left side of the join. It is very similar to LEFT JOIN for the rows for which there is
no matching row on the left side, the result-set will contain null. RIGHT JOIN is also known as
RIGHT OUTER JOIN.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Key Terms
Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are the same
Right JOIN
In this example, the RIGHT JOIN retrieves all rows from the StudentCourse table and the
matching rows from the Student table based on the ROLL_NO column.
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output
Output
FULL JOIN creates the result-set by combining results of both LEFT JOIN and RIGHT JOIN. The
result-set will contain all the rows from both tables. For the rows for which there is no matching,
the result-set will contain NULL values.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Key Terms
• table1: First table.
This example demonstrates the use of a FULL JOIN, which combines the results of both LEFT
JOIN and RIGHT JOIN. The query retrieves all rows from the Student and StudentCourse tables.
If a record in one table does not have a matching record in the other table, the result set will
include that record with NULL values for the missing fields
Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output
NAME COURSE_ID
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NAME COURSE_ID
NULL 4
NULL 5
NULL 4
A Natural Join is a type of INNER JOIN that automatically joins two tables based on columns
with the same name and data type. It returns only the rows where the values in the common
columns match.
• It returns rows where the values in these common columns are the same in both tables.
• Common columns appear only once in the result, even if they exist in both tables.
• Unlike a CROSS JOIN, which creates all possible combinations of rows, a Natural Join
only includes rows with matching values
Example:
Employee
1 Ram 10
2 Jon 30
3 Bob 50
Department
Dept_id Dept_name
Department
10 IT
30 HR
40 TIS
(Employee) ? (Department)
Output:
1 Ram 10 10 IT
2 Jon 30 30 HR
In SQL, the CROSS JOIN is a unique join operation that returns the Cartesian product of two or
more tables. This means it matches each row from the left table with every row from the right
table, resulting in a combination of all possible pairs of records.
In this article, we will learn the CROSS JOIN operation, its syntax, and practical examples to
clarify how and when to use it effectively.
Cross Join in SQL produces a result set that contains the cartesian product of two or more
tables. Cross join is also called a Cartesian Join. When CROSS JOIN is used with a WHERE
clause, it behaves like INNER JOIN, filtering the results based on specific conditions. CROSS
JOIN is the best choice when we need to match each row of one table to every other row of
another table. It is helpful in many applications where we need to obtain paired combinations of
records.
Cross Join B/W Two Sets
Syntax:
Let's look at some examples of CROSS JOIN statement in SQL to understand it's working.
In this CROSS JOIN tutorial, we will use the following two tables in examples:
Table 1- Customer
Table 2- Orders
To create both these tables on your system, you can write the following code:
Query:
SELECT *
FROM CUSTOMER
CROSS JOIN ORDERS;
Output:
Cross Join
As we can see, whether the other table matches or not, the CROSS JOIN keyword returns all
similar records from both tables. Therefore, if there are rows in "Customers" or "Orders" that do
not match any entries in either table, those rows will also be listed.
• CROSS JOIN performs the cross-product of records from two or more joined tables.
• SQL CROSS JOIN with condition of WHERE Clause operates as an INNER JOIN; when
used without one, it produces the cartesian product of all the rows from all the tables
provided in the SQL query.
• CROSS JOIN is different from other join types like INNER JOIN, LEFT JOIN, and RIGHT
JOIN, as it does not require a matching condition between the tables.
Conclusion
The SQL CROSS JOIN is a powerful tool for generating all possible combinations of rows from
two or more tables. It is particularly useful in scenarios where every record needs to be paired
with every other record, such as in creating combinations or generating test data. However, it
should be used with caution, especially with large datasets, as it can quickly result in huge
result sets that could impact performance. By understanding when and how to use CROSS
JOIN, you can harness its power to handle complex queries and generate diverse datasets for a
wide range of applications.
SQL | Date Functions (Set-1)
SQL Date Functions are essential for managing and manipulating date and time values in SQL
databases. They provide tools to perform operations such as calculating date differences,
retrieving current dates and times and formatting dates. From tracking sales trends to
calculating project deadlines, working with temporal data is central to decision-making and
reporting.
SQL Date Functions are built-in utilities provided by SQL databases to perform operations on
date and time values. These functions simplify handling temporal data, making them
indispensable for tasks like calculating intervals, extracting year or month values, and
formatting dates for display.
Let's use the salestable as the base for demonstrating each of the SQL Date
Functions mentioned. Here’s the structure of the sales table:
Sales Table
1. NOW()
The NOW() function returns the current date and time based on the server’s time zone. It's
commonly used when we need to capture the exact moment an event occurs, such as
a transaction timestamp. This function is useful for logging timestamps or comparing current
time with database records.
Query:
Output
current_datetime
2024-08-12 14:35:27
2. CURDATE()
The CURDATE() function returns the current date in the YYYY-MM-DD format. It is useful when
we need to retrieve only the current date without the time. This function is often used in
reporting or filtering records by date.
Query:
Output
current_date
2024-08-12
3. CURTIME()
The CURTIME() function returns the current time in the HH:MM:SS format. It is useful for time-
specific operations, such as scheduling tasks. By isolating the time, this function helps in
scenarios requiring precise time comparisons.
Query:
Output
current_time
14:35:27
4. DATE()
The DATE() function extracts the date portion from a date or datetime expression. This function
is useful when we want to ignore the time component and focus only on the date. This function
is commonly employed in date-only comparisons or aggregations.
Query:
Output
Date Function
5. EXTRACT()
The EXTRACT() function allows us to retrieve a specific part (like year, month, or day) from a
date. It's particularly useful when we need to group or filter data based on specific time
components. This function is especially useful in reports that require year-over-year analysis.
Query:
Output
Extract Function
6. DATE_ADD()
The DATE_ADD() function adds a specified time interval (like days, months, or years) to a date.
It's often used in scenarios where we need to calculate a future date. This function simplifies
future date calculations for planning purposes.
Query:
Output
sale_id product_name sale_date_plus_7_days
1 Widget A 2024-08-08
2 Widget B 2024-08-12
3 Widget C 2024-08-14
4 Widget A 2024-08-17
5 Widget B 2024-08-22
6 Widget C 2024-08-27
7. DATE_SUB()
The DATE_SUB()function subtracts a specified time interval from a date. It's handy when we
need to determine a past date by subtracting days, months, or years. This is often used for
retrospective data analysis.
Query:
Output
1 Widget A 2024-07-29
2 Widget B 2024-08-02
3 Widget C 2024-08-04
sale_id product_name sale_date_minus_3_days
4 Widget A 2024-08-07
5 Widget B 2024-08-12
6 Widget C 2024-08-17
8. DATEDIFF()
The DATEDIFF() function returns the difference in days between two dates. It's commonly used
to calculate the duration between two events or dates. This function is ideal for deadline
tracking or overdue calculations. Here we will find out how many days are left from each sale
till August 15, 2024.
Query:
Output
1 Widget A 2024-08-01 14
2 Widget B 2024-08-05 10
3 Widget C 2024-08-07 8
4 Widget A 2024-08-10 5
5 Widget B 2024-08-15 0
6 Widget C 2024-08-20 -5
9. DATE_FORMAT()
The DATE_FORMAT() function formats a date according to a specified format, allowing
for customized date output (e.g. displaying the full day name, month name etc). This function
is excellent for improving report readability.
Query:
Output
10. ADDDATE()
Query:
Output
sale_id product_name sale_date_plus_10_days
1 Widget A 2024-08-11
2 Widget B 2024-08-15
3 Widget C 2024-08-17
4 Widget A 2024-08-20
5 Widget B 2024-08-25
6 Widget C 2024-08-30
11. ADDTIME()
The ADDTIME()function adds a specified time interval to a time or datetime value. It is useful for
adjusting times by adding hours, minutes or seconds.
Query:
Output
1 Widget A 13:00:00
2 Widget B 13:00:00
3 Widget C 13:00:00
4 Widget A 13:00:00
sale_id product_name sale_time_plus_2hrs_30min
5 Widget B 13:00:00
6 Widget C 13:00:00
Conclusion
SQL Date Functions are powerful tools for managing and analysing temporal data. They
streamline operations ranging from simple date extractions to complex interval calculations.
Mastering these functions empowers users to derive meaningful insights and create accurate,
dynamic reports. By integrating these functions into our queries, we can enhance both
efficiency and clarity in handling date and time data.
SQL String Functions are powerful tools that allow us to manipulate, format, and extract
specific parts of text data in our database. These functions are essential for tasks like cleaning
up data, comparing strings, and combining text fields. Whether we're working
with names, addresses, or any form of textual data, mastering SQL string functions is crucial
for efficient data handling and analysis.
String functions are used to perform an operation on input string and return an output string.
Below are some of the most commonly used SQL string functions:
The CONCAT() function is used to concatenate (combine) two or more strings into one string. It
is useful when we want to merge fields like first and last names into a full name.
Query:
Output:
John Doe
The CHAR_LENGTH() or LENGTH() function returns the length of a string in characters. It’s
essential for validating or manipulating text data, especially when you need to know how many
characters a string contains.
Query:
Output:
5
These functions convert the text to uppercase or lowercase, respectively. They are useful for
normalizing the case of text in a database.
Query:
Output:
HELLO
hello
LENGTH() returns the length of a string in bytes. This can be useful for working with multi-byte
character sets.
Query:
Output:
The REPLACE() function replaces occurrences of a substring within a string with another
substring. This is useful for cleaning up data, such as replacing invalid characters or formatting
errors.
Query:
Output:
Hello SQL
The SUBSTRING() (or SUBSTR()) function is used to extract a substring from a string, starting
from a specified position. It is especially useful when we need to extract a specific part of a
string, like extracting the domain from an email address.
Query:
Output:
Hello
Query:
Output:
Hello
World
The INSTR() function is used to find the position of the first occurrence of a substring within a
string. It returns the position (1-based index) of the substring. If the substring is not found, it
returns 0. This function is particularly useful for locating specific characters or substrings in text
data.
Query:
Output:
The TRIM() function removes leading and trailing spaces (or other specified characters) from a
string. By default, it trims spaces but can also remove specific characters using TRIM(character
FROM string). This is helpful for cleaning text data, such as user inputs or database records.
Query:
Output:
Hello World
The REVERSE() function reverses the characters in a string. It’s useful in situations where we
need to process data backward, such as for password validation or certain pattern matching.
Query:
Output:
olleH
The ASCII() function returns the ASCII value of a single character. This is helpful when we need
to find the numeric code corresponding to a character, often used in encoding and decoding
text.
Syntax:
SELECT ascii('t');
Output:
116
CONCAT_WS() stands for "Concatenate With Separator." It allows us to join multiple strings
with a specific separator between them. This is ideal when we need to merge columns like first
name and last name with a custom separator.
Syntax:
Output:
geeks_for_geeks
The FIND_IN_SET() function returns the position of a value within a comma-separated list. This
is especially useful for finding out where an element exists in a string of values (e.g., tags,
categories).
Syntax:
Output:
The FORMAT() function is used to format a number as a string in a specific way, often with
commas for thousands or with a specific number of decimal places. It's handy when you need
to display numbers in a user-friendly format.
Syntax:
Output:
‘98.10%’
The INSTR() function returns the position of the first occurrence of a substring within a string. If
the substring is not found, it returns 0. It's useful for finding where specific text appears in a
larger string.
Syntax:
Output:
The LCASE() function converts all characters in a string to lowercase. It helps standardize text
data, especially when comparing strings in a case-insensitive way.
Syntax:
Output:
geeksforgeeks to learn
LOCATE() allows you to find the nth occurrence of a substring in a string. This is especially
useful when you need to locate a specific substring based on its position.
Syntax:
Output:
LPAD() is used to pad a string to a certain length by adding characters to the left side of the
original string. It's useful when you need to format data to a fixed length.
Syntax:
Output:
000geeks
MID() extracts a substring starting from a given position in a string and for a specified length. It's
useful when you want to extract a specific portion of a string.
Syntax:
Output:
for
The POSITION() function finds the position of the first occurrence of a specified character in a
string.
Syntax:
Output:
The REPEAT() function repeats a string a specified number of times. It's useful when you need to
duplicate a string or pattern for certain operations.
Syntax:
Output:
geeksgeeks
REPLACE() is used to replace all occurrences of a substring with another substring. It's useful
for replacing or cleaning up certain text in your data.
Syntax:
REPLACE('123geeks123', '123');
Output:
geeks
RPAD() pads the right side of a string with specified characters to a fixed length. This is often
used to format text or numbers to a desired size.
Syntax:
RPAD('geeks', 8, '0');
Output:
‘geeks000’
24. RTRIM(): Remove Trailing Characters
RTRIM() removes trailing characters from the right side of a string. By default, it removes spaces,
but you can specify other characters as well.
Syntax:
RTRIM('geeksxyxzyyy', 'xyz');
Output:
‘geeks’
The SPACE() function generates a string consisting of a specified number of spaces. This is
useful when you need to format output or create padding in your queries.
Syntax:
SELECT SPACE(7);
Output:
‘ ‘
STRCMP() compares two strings and returns an integer value based on their lexicographical
comparison. This is useful for sorting or checking equality between two strings.
STRCMP(string1, string2) returns:
Syntax
Output:
SELECT FIND_IN_SET('b',
FIND_IN_SET() Find position in a set. 2
'a,b,c');
SELECT LOCATE('e',
LOCATE() Find nth occurrence. 2
'geeksforgeeks', 1);
LPAD() Pad string from the left. SELECT LPAD('geeks', 8, '0'); 000geeks
SELECT POSITION('e' IN
POSITION() Find character position. 2
'geeks');
Conclusion
SQL String Functions are powerful tools for manipulating and analyzing string data in
databases. Whether we need to concatenate, extract, compare, or modify strings, these
functions provide the flexibility to handle a wide variety of string-related tasks. Understanding
and applying these functions can make our SQL queries more efficient and help us manipulate
data exactly as we need.
In SQL, constraints are used to enforce rules on data, ensuring the accuracy, consistency, and
integrity of the data stored in a database. One of the most commonly used constraints is
the NOT NULL constraint, which ensures that a column cannot have NULL values.
This is important for maintaining data integrity, especially when specific data entries are
mandatory. In this article, we will explore the SQL NOT NULL constraint in detail, its syntax, and
how it can be applied to your tables.
This constraint is similar to a primary key constraint in that both prevent NULL values. However,
they are different in their purpose and application. A primary key uniquely identifies each record
in a table, while the NOT NULL constraint simply ensures that a column cannot have empty or
undefined values.
Key Points:
• It can be used during table creation or modification (with the ALTER command).
Syntax:
....
);
For example, the "EMPID" will not accept NULL values when the EMPLOYEES table is created
because NOT NULL constraints are used with these columns.
Query:
Country VARCHAR(50),
Age int(2),
Salary int(10));
Output:
SQL NOT NULL on ALTER Table
We can also add a NOT NULL constraint in the existing table using the ALTER statement. For
example, if the EMPLOYEES table has already been created then add NOT NULL constraints to
the "Name" column using ALTER statements in SQL as follows:
Query:
This command ensures that the Name column will no longer accept NULL values, enforcing a
requirement for all employee records to have a valid name.
• Prevents Data Gaps: With NOT NULL, you avoid incomplete or missing data, which is
crucial for accurate data analysis.
• Enforces Business Logic: Often, business rules require certain fields to be mandatory.
For example, an employee record must have an employee ID, name, and department.
• Improves Data Integrity: By preventing null entries, you ensure that critical data is
always present, improving overall database reliability.
Conclusion
The SQL NOT NULL constraint is a powerful tool that ensures the integrity and completeness of
your database by preventing NULL values in critical columns. Whether you’re creating a new
table or modifying an existing one, applying the NOT NULL constraint guarantees that important
data is always present, which is essential for accurate reporting, analysis, and consistency
across your database. Always consider which fields are required before inserting records into
your tables to enforce data integrity with this constraint.
The PRIMARY KEY constraint in SQL is one of the most important constraints used to
ensure data integrity in a database table. A primary key uniquely identifies each record in a
table, preventing duplicate or NULL values in the specified column(s). Understanding how to
properly implement and use the primary key constraint is crucial for managing relational data
effectively.
There can only be one primary key in a table, but that primary key can consist of one or more
columns. When there are two or more columns in the primary key it is called a composite key.
A primary key automatically has a UNIQUE constraint defined on it, and it ensures that there
are no duplicate or NULL values in that column.
1. No duplicate values are allowed, i.e. The column assigned as the primary key should
have UNIQUE values only.
2. NO NULL values are present in the Primary key column. Hence there is a Mandatory
value in the column having the Primary key.
3. Only one primary key per table exists although the Primary key may have multiple
columns.
4. No new row can be inserted with the already existing primary key.
5. Primary keys can be classified into two categories Simple primary key that consists of
one column and composite primary key that consists of Multiple column.
Syntax
Let's look at some examples of the PRIMARY KEY Constraint in SQL, and understand it's
working.
Create PRIMARY KEY in SQL Example
In this example, we will create primary key in a new table using CREATE TABLE statement.
Query
FirstName varchar(255),
Age int
);
To verify if the primary key has been successfully created, we will try adding duplicate values in
primary key column, and SQL should return an error.
Query
Output
In this example, we will add primary key to a already existing table using ALTER TABLE
command.
Let's consider previous table, and create it without primary key this time.
PersonID int,
• A primary key is a column or a set of columns in a table that uniquely identifies each row.
• Creating a primary key automatically creates a unique index on the key column(s),
improving query performance.
• Establishing relationships between tables using SQL primary key and foreign key
improve database design, reduce data redundancy, and improve data consistency.
• Data Integrity: The primary key enforces data integrity by ensuring each record is
unique.
• Efficient Querying: Since a primary key automatically creates an index, querying for
records by the primary key is faster.
• Referential Integrity: Primary keys are used to establish relationships between tables
(via foreign keys), ensuring consistency across related data.
• Avoid NULL values: Always ensure that the columns involved in the primary key do not
accept NULL values.
• Choose meaningful primary keys: If possible, choose a primary key that naturally fits
the data and serves as a meaningful identifier, like an ID field.
• Composite Keys: Be cautious when using composite keys. While they are useful in
some scenarios, they can make queries more complex. If possible, use a simple key or
generate an artificial primary key (like an ID).
• Changing Primary Keys: Once a primary key is established, changing it can be difficult
because of the interdependencies with other tables (foreign key constraints). Always
plan ahead when designing your database schema.
Conclusion
The PRIMARY KEY constraint is a fundamental concept in relational databases that ensures
each record in a table is unique and identifiable. By using the primary key effectively, you can
maintain data integrity, improve query performance, and establish meaningful relationships
between tables. Whether you are working with simple or composite keys, it is important to
understand the properties and best practices to ensure a well-designed and efficient database
structure.
The SUM() function in SQL is one of the most commonly used aggregate functions. It allows us
to calculate the total sum of a numeric column, making it essential for reporting and data
analysis tasks. Whether we're working with sales data, financial figures, or any other numeric
information, the SUM() function can help us quickly compute the sum of values based on
specific conditions.
In this article, we will explain the SUM() function in detail, provide multiple examples, and
highlight its use in various SQL queries to enhance our understanding.
The SUM() function in SQL is used to calculate the total of a numeric column or expression.
This aggregate function sums the values in the specified column and returns a single result. It is
commonly used in combination with other SQL clauses like WHERE, GROUP BY,
and HAVING to refine the data set and calculate sums based on specific conditions.
Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Key Terms
• table_name: The name of the table from which to retrieve the data.
• condition: (Optional) A condition to filter the rows before performing the aggregation.
In this section, we will demonstrate the usage of the SUM() function with examples using a
sample table called Sales, which stores sales data such as the Product, Quantity, and Price.
This simple dataset will help us understand how the SUM() function works in SQL to calculate
totals, sums of distinct values, and more.
Sales Table
In this example, we will use the SUM() function to calculate the total value of a specific column,
such as total sales or total salary.
Query:
Output
TotalSalary
450,000
Explanation:
This query calculates the sum of the Salary column in the Employees table. This output shows
the total salary paid to employees in the database.
We can also use the SUM() function with an expression to calculate sums based on
some logic or mathematical operations.
Query:
Output
TotalRevenue
1,200,000
Explanation:
This query multiplies Price and Quantity for each record in the Sales table and then calculates
the sum of those values. This is useful for calculating the total revenue generated from sales.
When we want to calculate the sum of values within groups, we can use the GROUP BY clause
along with SUM(). This is particularly useful for grouping data by categories such
as departments, products, or cities.
Query:
Output
Department DepartmentSalary
HR 200,000
Department DepartmentSalary
Sales 300,000
IT 250,000
Explanation:
This query groups employees by their Department and then calculates the total salary for each
department.
If we want to sum only the distinct values in a column, we can use the DISTINCT keyword with
the SUM() function.
Query:
Output:
TotalDistinctPrice
500,000
Explanation:
This query sums only the unique values in the Price column of the Products table. Duplicate
values are excluded from the sum.
The HAVING clause can be used in combination with GROUP BY to filter groups based on the
result of the SUM() function. This allows you to apply conditions to the grouped data after the
aggregation.
Query:
Output
Department DepartmentSalary
Sales 300,000
IT 250,000
Explanation:
This query calculates the total salary per department and then filters the result to include only
those departments where the total salary is greater than 200,000.
• Use with Indexes: When summing a large dataset, it’s important to have indexes on the
columns you’re filtering by, such as dates or categories. This will improve the
performance of your query.
• Use GROUP BY to Categorize Data: The SUM() function works perfectly with GROUP
BY. It helps you summarize data efficiently by different categories like departments or
regions.
• Avoid Summing Non-Numeric Values: Ensure that the column you are summing
contains only numeric values. Summing non-numeric values can result in errors.
• Consider Using Aliases: Always use aliases for SUM() results for better readability and
clarity in your output.
Conclusion
The SQL SUM() function is a powerful tool for aggregating numeric data. Whether we need to
calculate the total salary, revenue, or count items, the SUM() function simplifies these tasks
and helps us derive valuable insights from our database. By using it with different clauses
like DISTINCT, GROUP BY, and HAVING, we can tailor our queries to specific conditions,
making our analysis more efficient. The SUM() function is especially useful for generating
summary reports and analyzing financial, sales, or inventory data.
In this article, we will cover the syntax, use cases, sample tables, and practical examples with
detailed explanations of how to use the MAX() function effectively. Whether we are a beginner
or an advanced SQL user, this guide will help us understand and apply
the MAX() function confidently.
Syntax:
SELECT MAX(column_name)
FROM table_name
[WHERE condition];
Key Terms:
• column_name: The column for which you want to find the maximum value.
In this section, we will demonstrate the usage of the MAX() function with examples using a two
sample tables. Product Prices table contains product IDs and their corresponding prices.
The Sales table contains sales data with sale IDs, product IDs, sale dates, and amounts.
Sales Table
Sales Table
To find the highest price in the product_prices table. This query is simple yet effective for
identifying the most expensive product in the dataset. It excludes NULL values, ensuring only
valid prices are considered.
Query:
Highest Price
199.99
Explanation: The MAX(price) function evaluates all the values in the price column and returns
the largest value, $199.99.
To determine the highest sale amount from the Sales table. This query is commonly used
in sales data analysis to find the most significant transaction in terms of value. It helps
businesses track their peak sales performances.
Query:
Output:
Highest Sale
1500.00
Explanation: The MAX(amount) function scans the amount column in the Sales table and
identifies the largest sale, which is $1500.00
Find the maximum price for products under a specific category, e.g., Electronics: This query is
valuable for targeted analysis such as identifying the product with highest price within a
specific category.
Query:
Output:
149.95
Explanation: The WHERE clause filters rows to include only products in the 'Electronics'
category. The MAX(price) function then calculates the maximum price within this subset.
Example 4: Find the Latest Sale Date
To find the most recent sale date in the Sales table. This query is particularly useful for tracking
the last activity in a dataset, such as identifying the latest transaction or update.
Query:
Output:
2023-02-01
Explanation: The MAX(sale_date) function evaluates all dates in the sale_date column and
returns the most recent one, 2023-02-01
Find the highest sale amount for each product. This query is helpful for analyzing performance
metrics at the product level, allowing businesses to track the best-performing products.
Query:
1 500.00
2 1500.00
3 300.00
Explanation: The GROUP BY clause groups rows by product_id, and the MAX(amount) function
calculates the highest sale amount for each group.
Retrieve details of the product with the highest price. The main query retrieves the full record
associated with this maximum price, providing detailed information about the most expensive
product.
Query:
SELECT *
FROM product_prices
WHERE price = (SELECT MAX(price) FROM product_prices);
Output:
product_id price
4 199.99
Explanation: The subquery (SELECT MAX(price) FROM product_prices) determines the highest
price ($199.99). The main query fetches all records matching this price.
Conclusion
The MAX() function in SQL is a fundamental tool for data analysis, enabling users to find the
highest value in a column. It can be used independently or in combination with other SQL
clauses like WHERE, GROUP BY, and subqueries to handle complex data requirements. By
mastering the MAX() function, WE can efficiently analyze and summarize data in various
scenarios, such as identifying maximum prices, latest dates, or highest scores.
SQL is an RDBMS system in which SQL functions become very essential to provide us with
primary data insights. One of the most important functions is called AVG() and is particularly
useful for the calculation of averages within datasets.
In this, we will learn about the AVG() function, and its syntax by understanding various examples
and their output with explanation and so on.
The SQL function AVG() is designed for calculating the average value of any numeric column
within a certain data set. It does this by adding up all the values of the column and then dividing
the resulting number by the number of non-null values of the column.
Thus, the function is best suited to propose a typical value of a given data set which helps to
analyze the data-set features.
Syntax:
SELECT AVG(column_name)
FROM table_name;
Here, column_name represents the column from which you want to compute the average,
and table_name is the name of the table containing the data. Optionally, you can use
the WHERE clause to specify conditions for filtering the data before calculating the average.
Set Up an Environment
CREATE TABLE student_scores (
student_id INT,
subject VARCHAR(50),
score INT
);
Output:
Output:
| subject | average_score |
|------------|---------------|
| Math | 83.3333 |
| Science | 80 |
| English | 88.3333 |
Explanation: In this example, we're using the AVG() function to compute the average score for
each subject. The GROUP BY clause is used to group the results by the subject column. The
output will display two columns: subject and average_score.
Output:
| overall_average_score |
|-----------------------|
| 83.88888888888889 |
Explanation: Here, we're computing the average score across all subjects using the AVG()
function without any grouping. This will give us a single value representing the overall average
score of all students
Output:
| average_science_score |
|-----------------------|
| 80 |
Explanation: In this example, we're filtering the data using the WHERE clause to focus only on
the "Science" subject. Then, we use the AVG() function to calculate the average score for that
specific subject
Considerations
While the AVG() function is a powerful tool for data analysis, there are some considerations to
keep in mind:
• Handling NULL Values: AVG () function by default ignore NULL values when calculating
the total. However, they have to be careful in how they interpret the results of the study,
especially when missing values are relevant to your study.
• Data Type Compatibility: Make sure the function of AVG() is applied to the column that
contains numbers as the values. The usage of it beyond numerical columns will result in
erroneous results hence.
• Precision and Rounding: Since rounding is used in different cases, you can either keep
the result of the AVG() function as it is, or you may round it to a specific number of
decimal places so that the result is clear and consistent
Conclusion
In SQL, the AVG() function is a good function for studying numerical data. It enables analysts as
well as data experts to carry out easy calculations and thereby give a clear understanding
regarding various vital factors in their datasets. Whether you are deciding on sales figures,
evaluating student performance or monitoring website statistics AVG() gives you the statistics
with facts on which you can depend when making important decisions.
SQL Subquery
A subquery in SQL is a query nested within another SQL query. It allows you to perform complex
filtering, aggregation, and data manipulation by using the result of one query inside another.
Subqueries are often found in the WHERE, HAVING, or FROM clauses and are supported in
SELECT, INSERT, UPDATE, and DELETE statements.
• Filtering: Getting specific records based on conditions derived from another query.
• Deleting: Deleting records from one table using criteria based on another.
While there is no universal syntax for subqueries, they are commonly used
in SELECT statements as follows. This general syntax allows the outer query to use the results of
the inner subquery for filtering or other operations.
Syntax
SELECT column_name
FROM table_name
WHERE column_name expression operator
(SELECT column_name FROM table_name WHERE ...);
3. Comparison Operators: Subqueries can be used with operators like =, >, <, IN, NOT
IN, LIKE, etc.
4. Single-Row vs. Multi-Row Subqueries: Subqueries may return a single value (e.g., a
single row) or multiple values. Depending on the result, different SQL constructs may be
required.
Subqueries are frequently used in specific SQL clauses to achieve more complex results. Here
are the common clauses where subqueries are used:
• WHERE Clause: Filter rows based on subquery results. For example, you can filter
records based on values returned by a subquery.
• FROM Clause: Subqueries can be used in the FROM clause to treat the result of the
subquery as a derived table or temporary table that can be joined with other tables.
Types of Subqueries
1. Single-Row Subquery: Returns a single value (row). Useful with comparison operators
like =, >, <.
3. Correlated Subquery: Refers to columns from the outer query in the subquery. Unlike
regular subqueries, the subquery depends on the outer query for its values.
4. Non-Correlated Subquery: Does not refer to the outer query and can be executed
independently.
These examples showcase how subqueries can be used for various operations
like selecting, updating, deleting, or inserting data, providing insights into
their syntax and functionality. Through these examples, we will understand the flexibility and
importance of subqueries in simplifying complex database tasks. Consider the following two
tables:
1. DATABASE TABLE
Database Table
2. STUDENT TABLE
Student Table
This example demonstrates how to use a subquery to retrieves roll numbers of students
in section 'A', and the outer query uses those roll numbers to fetch corresponding details
(name, location, and phone number) from the DATABASE table. This enables filtering based on
results from another table.
Query:
Output
Explanation: The inner query fetches the roll numbers of students in section 'A'. The outer query
uses those roll numbers to filter records from the DATABASE table.
In this example, a subquery is used to insert all records from the Student2 table into
the Student1 table. The SELECT statement inside the INSERT INTO statement fetches all the
data from Student2 and inserts it into Student1.
Student1 Table
Student2 Table
Query:
Output
Explanation: The SELECT statement inside the INSERT INTO query fetches all records
from Student2 and inserts them into Student1.
Subqueries can be used in DELETE statements to remove rows based on data from another
table. In this example, the subquery gets roll numbers from Student1 where the location is
'Chennai'. The outer query then deletes matching rows from Student2.
Query:
Output
Explanation: The subquery retrieves roll numbers of students from Student1 who are located in
'Chennai'. The outer query deletes those records from Student2.
The subquery retrieves the locations of 'Raju' and 'Ravi' from Student1. The outer query then
updates the NAME in Student2 to 'Geeks' for all students whose LOCATION matches any of the
retrieved locations. This allows for updating data in Student2 based on conditions from
Student1.
Query:
UPDATE Student2
SET NAME='geeks'
WHERE LOCATION IN (SELECT LOCATION
FROM Student1
WHERE NAME IN ('Raju', 'Ravi'));
Output
Explanation: The inner query fetches the locations of 'Raju' and 'Ravi' from Student1. The outer
query updates the name to 'Geeks' in Student2 where the location matches those of 'Raju' or
'Ravi'
While subqueries are powerful, here are some best practices to ensure your queries are
efficient and maintainable:
1. Avoid Nested Subqueries When Possible: Too many nested subqueries can slow down
performance. Consider using JOIN operations if applicable.
2. Use EXISTS Instead of IN for Better Performance: When dealing with large
datasets, EXISTS can sometimes be more efficient than IN, especially when the
subquery returns a large result set.
3. Use Aliases for Clarity: When using subqueries in the FROM clause, always use aliases
to make your query more readable and maintainable.
4. Test with Different Scenarios: Always test your subqueries in different environments
(e.g., development vs. production) to ensure they perform well
SQL window functions are essential for advanced data analysis and database management. It is
a type of function that allows us to perform calculations across a specific set of rows related to
the current row. These calculations happen within a defined window of data and they are
particularly useful for aggregates, rankings and cumulative totals without modifying the dataset.
The OVER clause is a key for defining this window. It partitions the data into different sets (using
the PARTITION BY clause) and orders them (using the ORDER BY clause). These windows enable
functions like SUM(), AVG(), ROW_NUMBER(), RANK() and DENSE_RANK() to be applied in an
organized manner.
Syntax
SELECT column_name1,
window_function(column_name2)
OVER([PARTITION BY column_name1] [ORDER BY column_name3]) AS new_column
FROM table_name;
Key Terms
SQL window functions can be categorized into two primary types: aggregate window
functions and ranking window functions. These two types serve different purposes but share a
common ability to perform calculations over a defined set of rows while retaining the original
data. The employee table contains details about employees, such as their name, age,
department and salary.
Employees Table
Employees Table
Aggregate window functions calculate aggregates over a window of rows while retaining
individual rows. Common aggregate functions include:
Example: Using AVG() to Calculate the Average Salary within each department
Output
AVG() Function
Explanation:
• The AVG() function calculates the average salary for each department using
the PARTITION BY Department clause.
• The average salary is repeated for all rows in the respective department.
These functions provide rankings of rows within a partition based on specific criteria. Common
ranking functions include:
• DENSE_RANK(): Assigns ranks to rows without skipping rank numbers for duplicates.
RANK() Function
The RANK() function assigns ranks to rows within a partition, with the same rank given to rows
with identical values. If two rows share the same rank, the next rank is skipped.
Output
RANK() Function
Explanation:
Rows with the same salary (e.g., Ramesh and Suresh) are assigned the same rank. The next rank
is skipped (e.g., rank 2) due to duplicate ranks.
DENSE_RANK() Function
It assigns rank to each row within partition. Just like rank function first row is assigned rank 1
and rows having same value have same rank. The difference between RANK() and
DENSE_RANK() is that in DENSE_RANK(), for the next rank after two same rank, consecutive
integer is used, no rank is skipped.
Example:
Output
Explanation: The DENSE_RANK() function works similarly to RANK(), but it doesn't skip rank
numbers when there are ties. For example, if two employees have the same salary, both will
receive rank 1, and the next employee will receive rank 2.
ROW_NUMBER() Function
ROW_NUMBER() gives each row a unique number. It numbers rows from one to the total rows.
The rows are put into groups based on their values. Each group is called a partition. In each
partition, rows get numbers one after another. No two rows have the same number in a
partition.
Output
Window functions are extremely versatile and can be used in a variety of practical scenarios.
Below are some examples of how these functions can be applied in real-world data analysis.
We want to calculate a running total of sales for each day without resetting the total, every time
a new day starts.
Explanation: This query calculates the cumulative total of sales for each day, ordered by date.
We need to retrieve the top 3 employees in each department based on their salary.
WITH RankedEmployees AS (
SELECT Name, Department, Salary,
RANK() OVER(PARTITION BY Department ORDER BY Salary DESC) AS emp_rank
FROM employee
)
SELECT Name, Department, Salary
FROM RankedEmployees
WHERE emp_rank <= 3;
Explanation: This query retrieves the top 3 employees per department based on salary. It
uses RANK() to rank employees within each department and filters for the top 3
While SQL window functions are incredibly powerful, there are some common pitfalls and
challenges that users may encounter:
• Partitioning Error: Ensure that the PARTITION BY clause is used correctly. If no partition
is defined, the entire result set is treated as a single window.
• ORDER BY Within the Window: The ORDER BY clause within the window function
determines the order of calculations. Always verify that it aligns with the logic of your
calculation.
Conclusion
SQL window functions are a crucial feature for advanced data analysis and provide flexibility
when working with partitioned data. By mastering the OVER, PARTITION BY and ORDER
BY clause, we can perform complex calculations like aggregate calculations, ranking and
cumulative totals while preserving the row-level data. The combination of window functions
with ORDER BY and PARTITION BY provides a flexible approach for data manipulation across
different types of datasets.
Stored procedures are precompiled SQL statements that are stored in the database and can be
executed as a single unit. SQL Stored Procedures are a powerful feature in database
management systems (DBMS) that allow developers to encapsulate SQL code and business
logic. When executed, they can accept input parameters and return output, acting as a reusable
unit of work that can be invoked multiple times by users, applications, or other procedures.
Syntax:
Key Terms
• CREATE PROCEDURE: This keyword creates the stored procedure with the given name.
• @parameter1, @parameter2: These are input parameters that allow you to pass values
into the stored procedure.
• BEGIN...END: These keywords define the block of SQL statements that make up the
procedure body.
These are predefined stored procedures provided by the SQL Server for performing
administrative tasks such as database management, troubleshooting, or system configuration.
Examples include:
These are custom stored procedures created by the user to perform specific operations. User-
defined stored procedures can be tailored to a business's needs, such as calculating totals,
processing orders, or generating reports. For example, creating a procedure that calculates the
total sales for a particular product category.
These allow for the execution of external functions, which might be implemented in other
languages such as C or C++. Extended procedures provide a bridge between SQL Server and
external applications or tools, such as integrating third-party tools into SQL Server.
These are stored procedures written in .NET languages (like C#) and executed within SQL Server.
CLR stored procedures are useful when advanced functionality is needed that isn't easily
achievable with T-SQL alone, such as complex string manipulation or working with external
APIs.
There are several key reasons why SQL Stored Procedures are widely used in database
management:
2. Security and Data Access Control: By using stored procedures, developers can restrict
direct access to sensitive data. Users can execute procedures without accessing the
underlying tables, helping to protect critical information.
Customers Table
By passing a country as a parameter, the stored procedure dynamically fetches the relevant
customer details from the table
Query:
Output
Naveen Tulasi
Note: We will need to make sure that the user account has the necessary privileges to create
a database. We can try logging in as a different user with administrative privileges or contact
the database administrator to grant the necessary privileges to our user account. If we are using
a cloud-based database service, make sure that we have correctly configured the user account
and its permissions.
3. Code Reusability: Stored procedures allow for reusability, making it easier to maintain
and update code.
4. Reduced Network Traffic: By bundling multiple SQL statements into one call, stored
procedures reduce network load and improve application performance.
5. Better Error Handling: SQL stored procedures provide a structured way to manage
errors using TRY...CATCH blocks.
3. Data Validation Use stored procedures to validate data before it’s inserted into the
database. For example, checking if an email address already exists before adding a new
user.
4. Audit Logs Create a stored procedure that automatically logs changes to sensitive data,
such as changes to user roles or permissions, for security and auditing purposes.
Avoid making stored procedures too complex. Break up larger tasks into smaller, more
manageable procedures that can be combined as needed. This improves readability and
maintainability.
Always use TRY...CATCH blocks to handle exceptions gracefully. This ensures that errors are
caught and logged, and the procedure can handle unexpected scenarios without crashing.
While cursors can be useful, they are often less efficient than set-based operations. Use
cursors only when necessary, and consider alternatives like WHILE loops or CTEs (Common
Table Expressions).
Consider indexing, query optimization, and avoiding unnecessary joins within stored
procedures. Well-optimized queries in stored procedures ensure that performance does not
degrade as the database grows.
Conclusion
SQL stored procedures are an essential part of SQL development, offering benefits such as
improved performance, security, and maintainability. By encapsulating SQL queries into
reusable units, stored procedures simplify database management, enhance efficiency, and
ensure consistent business logic execution. By using stored procedures, we can automate
tasks, minimize the risk of SQL injection, and ensure consistent execution of complex SQL
logic. Stored procedures are integral to modern database management and an important
component in building scalable, efficient, and secure database systems.
SQL Triggers
In simple words, a trigger is a collection of SQL statements with particular names that are
stored in system memory. It belongs to a specific class of stored procedures that are
automatically invoked in response to database server events. Every trigger has a table attached
to it.
Syntax
Key Terms
• BEFORE | AFTER: Specifies whether the trigger is fired before or after the triggering
event (INSERT, UPDATE, DELETE).
• {INSERT | UPDATE | DELETE}: Specifies the operation that will activate the trigger.
• Automation: Triggers handle repetitive tasks, saving our time and effort.
• Consistency & Data Integrity: Automatically enforcing rules ensures that our data
remains clean and accurate.
• Business Rules Enforcement: Triggers can help ensure that changes to our database
follow your business logic.
• Audit Trails: Track changes automatically, making it easier to monitor and record data
updates.
Now that you know why triggers are important, let’s look at how to create and use them
effectively.
Triggers can be categorized into different types based on the action they are associated with:
1. DDL Triggers
The Data Definition Language (DDL) command events such as Create_table, Create_view,
drop_table, Drop_view, and Alter_table cause the DDL triggers to be activated. They allow us to
track changes in the structure of the database. The trigger will prevent any table creation,
alteration, or deletion in the database.
Output:
2. DML Triggers
DML triggers fire when we manipulate data with commands like INSERT, UPDATE, or DELETE.
These triggers are perfect for scenarios where we need to validate data before it is inserted, log
changes to a table, or cascade updates across related tables.
Example: Prevent Unauthorized Updates Let’s say you want to prevent users from updating the
data in a sensitive students table. We can set up a trigger to handle that:
Output
DML Trigger
3. Logon Triggers
These triggers are fired in response to logon events. Logon triggers are useful for monitoring user
sessions or restricting user access to the database. As a result, the PRINT statement messages
and any errors generated by the trigger will all be visible in the SQL Server error log.
Authentication errors prevent logon triggers from being used. These triggers can be used to track
login activity or set a limit on the number of sessions that a given login can have in order to audit
and manage server sessions.
Triggers can automatically perform tasks, like updating related tables when data changes.
Imagine we have a database for students, where the student_gradestable holds individual
subject grades. If the grade of a student is updated, we may also need to update
the total_scorestable.
CREATE TRIGGER update_student_score
AFTER UPDATE ON student_grades
FOR EACH ROW
BEGIN
UPDATE total_scores
SET score = score + :new.grade
WHERE student_id = :new.student_id;
END;
This ensures that every time a student's grade is updated, the total score in the total_scores
table is automatically recalculated.
Triggers can be used to validate data before it is inserted into a table, ensuring that the data
follows specific business rules. For instance, we may want to ensure that the grades being
inserted are within a valid range (say 0 to 100).
The trigger checks if the inserted grade is valid. If not, it throws an error and prevents the
insertion.
If we are working with many tables across multiple databases, we can use a simple query to list
all available triggers in our SQL Server instance. This is helpful for tracking and managing
triggers, especially when dealing with tables that have similar names across databases.
Key Terms
The SQL Server Management Studio makes it very simple to display or list all triggers that are
available for any given table. The following steps will help us accomplish this: Go to
the Databases menu, select the desired database, and then expand it.
• We will get various options here. When we choose the Triggers option, it displays all the
triggers available in this table.
SQL triggers can be specified to run BEFORE or AFTER the triggering event.
• BEFORE Triggers: These run before the action (INSERT, UPDATE, DELETE) is executed.
They’re great for data validation or modifying values before they are committed to the
database.
• AFTER Triggers: Execute after the SQL statement completes. Useful for logging or
cascading updates to other tables.
Given Student Report Database, in which student marks assessment is recorded. In such a
schema, create a trigger so that the total and percentage of specified marks are automatically
inserted whenever a record is inserted. Here, a trigger will invoke before the record is inserted
so BEFORE Tag can be used.
Query
mysql>>desc Student;
Student Datatype
Below SQL statement will create a trigger in the student database in which whenever subjects
marks are entered, before inserting this data into the database, the trigger will compute
those two values and insert them with the entered values. In this way, triggers can
be created and executed in the databases.
Stud_marks
Output:
Output
Advantages of Triggers
• Data Integrity: Triggers help enforce consistency and business rules, ensuring that data
follows the correct format.
• Audit Trail: Triggers can track changes in a database, providing an audit trail
of INSERT, UPDATE, and DELETE operations.
SQL performance tuning is an essential aspect of database management that helps improve
the efficiency of SQL queries and ensures that database systems run smoothly. Properly tuned
queries execute faster, reducing response times and minimizing the load on the server
In this article, we'll discuss various techniques, tools, and strategies used for SQL
performance tuning. By mastering these concepts, we can optimize our SQL queries and
achieve superior performance in our database.
What is SQL Performance Tuning?
Optimizing SQL performance is crucial because poorly optimized queries can severely affect
the speed of the database, increase CPU usage, and lead to system downtime. By improving
query execution times and resource utilization, performance tuning enhances the overall
performance of the SQL database.
Some of the major factors that influence the computation and execution time in SQL are:
• Table Size: Larger tables with millions of rows can slow down query performance if the
query hits a large number of rows.
• Joins: The use of complex joins, especially when joining multiple tables, can
significantly affect query execution time.
• Aggregations: Queries that aggregate large datasets require more processing time and
resources.
• Concurrency: Simultaneous queries from multiple users can overwhelm the database,
leading to slow performance.
• Indexes: Proper indexing speeds up data retrieval but, when misused, can lead to
inefficiencies.
SQL Server Management Studio allows users to view the execution plan, which details how SQL
Server processes a query. This plan helps identify inefficiencies like missing indexes or
unnecessary table scans. To create an execution plan:
• Start by selecting "Database Engine Query" from the toolbar of SQL Server
Management Studio.
• Enter the query after that, and then select "Include Actual Execution Plan" from the
Query option.
• It's time to run your query at this point. You can do that by pressing F5 or the "Execute"
toolbar button.
• The execution plan will then be shown in the results pane, under the "Execution Pane"
tab, in SQL Server Management Studio.
SQL Server's performance is closely tied to resource usage (CPU, memory, and disk).
Monitoring tools like Windows Performance Monitor can track these metrics and
highlight performance bottlenecks. We may view SQL Server objects, performance counters,
and other object activity with it. Simultaneously watch Windows and SQL Server counters with
System Monitor to see if there is any correlation between the two services' performance.
The abundance of dynamic management views (DMVs) that SQL Server includes is one of its
best features, helping identify slow-running queries, execution plans, and resource
consumption. DMVs such as sys.dm_exec_query_stats can be used to track query
performance.
Inefficient queries or those containing errors can consume excessive resources in the
production database, leading to slower performance or even disconnecting other users. It's
important to optimize queries to minimize their impact on overall database performance.
In this section, we’ll discuss several effective SQL performance tuning techniques, along with
practical examples, that can help optimize queries and improve database efficiency. These
methods focus on reducing resource consumption and improving execution speed, ensuring
a smoother and faster user experience.
Using SELECT * retrieves all columns from a table, but if you only need specific columns, this
can unnecessarily increase processing time. Instead, specify the columns needed. By using
the SELECT statement, one may direct the database to only query the data we actually need to
suit your business needs. For example:
Inefficient:
Efficient:
It is practical to get rid of duplicates from a query by using SELECT DISTINCT. To get separate
results, SELECT DISTINCT GROUPs for every field in the query. However, a lot of computing
power is needed to achieve this goal. Instead of using DISTINCT, refine your query to return
unique results naturally by adjusting the selection criteria.
Inefficient:
Efficient:
Inefficient:
Efficient:
The HAVING clause is used after aggregation and can be less efficient. When possible,
use WHERE to filter results before aggregation to speed up the query. A WHERE statement is
more effective if the goal is to filter a query based on conditions. Assuming 500 sales were made
in 2019, for instance, query to find how many sales were made per client that year.
Inefficient:
Efficient:
Wildcards enable the broadest search when searching unencrypted material, such as names
or cities. However, the most extensive search is also the least effective. Using wildcards
like % at the beginning of a string makes it difficult for SQL to efficiently use indexes. It's better
to place them at the end of the search term.
Inefficient:
Efficient:
Query:
Running heavy queries during off-peak hours reduces the load on the database, minimizing the
impact on other users. About planning any query to run at a time when it won't be as busy in
order to reduce the impact of our analytical queries on the database. When the number of
concurrent users is at its lowest, which is often overnight, the query should be executed.
Index Tuning
When choosing and building indexes, database tuning includes index tuning. The index tuning
objective is to speed up query processing. It can be challenging to employ indexes in dynamic
contexts with numerous ad-hoc searches scheduled in advance. The queries that are based on
indexes are subject to index tweaking, and the indexes are generated automatically as needed.
Users of the database do not need to take any specific activities to tune the index.
The performance of queries and databases can be improved by using the Index tuning wizard. It
accomplishes this using the following methods:
• Recommendations for optimal index usage based on query optimizer analysis and
workload
Several SQL performance tuning tools can help identify and optimize database performance.
Some of the popular tools include:
These tools assist with monitoring, identifying slow queries, and recommending optimization
strategies for improving database performance.
Conclusion
SQL performance tuning is important for ensuring efficient database operations, especially as
the size and complexity of databases grow. By utilizing best practices for query
optimization, indexing, and performance monitoring, we can significantly improve
our database’s response time and reduce system resource usage. Regular maintenance and
performance tuning should be an integral part of database management for businesses relying
on SQL databases for critical operations.
SQL TRANSACTIONS
SQL transactions are essential for ensuring data integrity and consistency in relational
databases. Transactions allow for a group of SQL operations to be executed as a single unit,
ensuring that either all the operations succeed or none of them do. Transactions allow us to
group SQL operations into a single, unified unit. But what exactly is a transaction, and why is it
so important in maintaining data integrity?
A transaction in SQL is a sequence of one or more SQL statements executed as a single unit of
work. These statements could be performing operations like INSERT, UPDATE, or DELETE. The
main goal of a transaction is to ensure that all operations within it should be either completed
successful or rolled back entirely if an error occurs.
The essence of SQL transactions lies in the concept of atomicity either all the operations within
the transaction succeed, or none of them do. This helps maintain the integrity and consistency
of the data in the database.
The integrity of SQL transactions is governed by the ACID properties, which guarantee reliable
database transactions. These four properties work together to guarantee that the database
remains consistent and reliable.
• Atomicity: The outcome of a transaction can either be completely successful or
completely unsuccessful. The whole transaction must be rolled back if one part of it
fails.
• Isolation: Concurrent transactions are isolated from one another, assuring the accuracy
of the data.
In SQL, transaction control commands manage the execution of SQL operations, ensuring the
integrity and reliability of database transactions. These commands help manage the start,
commit, and rollback of changes made to the database. Below are the key transaction control
commands in SQL, explained with syntax and examples for each.
The BEGIN TRANSACTION command marks the beginning of a new transaction. All SQL
statements that follow this command will be part of the same transaction until
a COMMIT or ROLLBACK is encountered. This command doesn't make any changes to the
database, it just starts the transaction.
Syntax:
Let’s look at an example of a bank transfer between two accounts. This example demonstrates
the usage of multiple queries in a single transaction.
BEGIN TRANSACTION;
If any error occurs, such as an issue with the UPDATE query, you can use ROLLBACK to undo all
changes made during the transaction:
ROLLBACK;
This ensures that the system doesn't end up in an inconsistent state, such as deducting money
from one account without adding it to another.
2. COMMIT Command
The COMMIT command is used to save all changes made during the current transaction to the
database. Once a transaction is committed, the changes are permanent.
Syntax:
COMMIT;
Example
Here is the sample Student table that will be used to perform the operations in this example.
This table contains basic student details such as ID, name, age, and other relevant information
that will be manipulated using various transaction control commands.
Student Table
Following is an example which would delete those records from the table which have age = 20
and then COMMIT the changes in the database.
Output
output
3. ROLLBACK Command
The ROLLBACK command is used to undo all changes made in the current transaction. It is
used when an error occurs or when the desired changes cannot be completed. The database
will revert to the state it was in before the BEGIN TRANSACTION was executed.
Syntax:
ROLLBACK;
Example
Delete those records from the table which have age = 20 and then ROLLBACK the changes in the
database. In this case, the DELETE operation is undone, and the changes to the database are
not saved.
Output
output
4. SAVEPOINT Command
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Example
SAVEPOINT SP1;
//Savepoint created.
DELETE FROM Student WHERE AGE = 20;
//deleted
SAVEPOINT SP2;
//Savepoint created.
Output
output
Explanation:
From the above example Sample table1, Delete those records from the table which have age =
20 and then ROLLBACK the changes in the database by keeping Savepoints. Here SP1 is first
SAVEPOINT created before deletion. In this example one deletion have taken place. After
deletion again SAVEPOINT SP2 is created.
5. ROLLBACK TO SAVEPOINT
The ROLLBACK TO SAVEPOINT command allows us to roll back the transaction to a specific
savepoint, effectively undoing changes made after that point.
Syntax:
Example
Deletion have been taken place, let us assume that we have changed our mind and decided to
ROLLBACK to the SAVEPOINT that we identified as SP1 which is before deletion. So, In this case
the DELETE operation is undone, and the transaction is returned to the state it was in at
the SP1 savepoint.
ROLLBACK TO SP1;
//Rollback completed
Output
output
This command is used to remove a SAVEPOINT that we have created. Once a SAVEPOINT has
been released, we can no longer use the ROLLBACK command to undo transactions performed
since the last SAVEPOINT. It is used to initiate a database transaction and used to specify
characteristics of the transaction that follows.
Syntax:
Example
Once the savepoint SP2 is released, we can no longer roll back to it.
In this case, without a transaction, you risk scenarios where money is deducted from one
account but not added to the other, leaving the system in an inconsistent state. Transactions
ensure that such issues are avoided by guaranteeing that both operations succeed or fail
together.
There are different types of transactions based on their nature and the specific operations they
perform:
• Read Transactions: Used to only read the data, typically with SELECT queries.
• Explicit Transactions: Manually controlled transactions where the user begins and
ends the transaction using BEGIN TRANSACTION, COMMIT, and ROLLBACK.
1. Monitor Locks: Track locking behavior and adjust queries to minimize locking conflicts.
2. Limit Transaction Scope: Limit the number of rows or records affected by a transaction to
speed up processing.
3. Use Batch Processing: If you're handling large amounts of data, break the operations into
smaller transactions or batches to avoid overwhelming the system.
While SQL transactions are powerful, they require careful management to ensure optimal
performance and avoid problems like transaction blocking and unnecessary locks. Here are
some best practices:
1. Keep Transactions Short: Avoid long-running transactions as they can lock tables and
reduce concurrency.
2. Use Proper Indexing: Proper indexing on tables involved in the transaction can greatly
improve performance.
3. Avoid Transaction Blocking: Ensure transactions don’t block others unnecessarily, causing
delays.
4. Commit Early: Try to commit as soon as the transaction is complete to release resources.
Conclusion