Structured Query Languages SQL
Structured Query Languages SQL
Important Terminologies
These are some important terminologies that are used in terms of relation.
Attribute:Attributes are the properties that define a relation.
e.g.; ROLL_NO, NAME etc.
Tuple:Each row in the relation is known as tuple. The above relation
contains 4 tuples, one of which is shown as:
1 RAM DELHI 9455123451 18
Degree:The number of attributes in the relation is known as degree
of the relation. The STUDENT relation defined above has degree 5.
Cardinality:The number of tuples in a relation is known as
cardinality. The STUDENTrelation defined above has cardinality 4.
Column:Column represents the set of values for a particular
attribute. The column ROLL_NO is extracted from relation STUDENT.
ROLL_NO
ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
4 SURESH
ROLL_NO NAME
3 SUJIT
4 SURESH
Note:
ORDER BY AGEis equivalent to ORDER BY AGE ASC.
If we want to retrieve the results in descending order of AGE, we can
use ORDER BY AGE DESC.
ADDRESS
DELHI
GURGAON
ROHTAK
If DISTINCT is not used, DELHI will be repeated twice in result set. Before
understanding GROUP BY and HAVING, we need to understand aggregations
functions in SQL.
Aggregation Functions
Aggregation functions are used to perform mathematical operations on data
values of a relation. Some of the common aggregation functions used in SQL
are:
COUNT: Count function is used to count the number of rows in a
relation. e.g;
SELECT COUNT (PHONE) FROM STUDENT;
COUNT(PHONE)
SUM(AGE)
74
In the same way, MIN, MAX and AVG can be used. As we have seen above, all
aggregation functions return only 1 row. AVERAGE: It gives the average values
of the tupples. It is also defined as sum divided by count values.
Syntax:
AVG(attributename)
OR
SUM(attributename)/COUNT(attributename)
The above mentioned syntax also retrieves the average value of tupples.
MAXIMUM:It extracts the maximum value among the set of tupples.
Syntax:
MAX(attributename)
MINIMUM:It extracts the minimum value amongst the set of all the
tupples.
Syntax:
MIN(attributename)
GROUP BY:Group by is used to group the tuples of a relation based
on an attribute or group of attribute. It is always combined with
aggregation function which is computed on group. e.g.;
SELECT ADDRESS, SUM(AGE) FROM STUDENT
GROUP BY (ADDRESS);
In this query, SUM(AGE) will be computed but not for entire table but for each
address. i.e.; sum of AGE for address DELHI(18+18=36) and similarly for other
address as well. The output is:
ADDRESS SUM(AGE)
DELHI 36
GURGAON 18
ROHTAK 20
If we try to execute the query given below, it will result in error because
although we have computed SUM(AGE) for each address, there are more than
1 ROLL_NO for each address we have grouped. So it can’t be displayed in
result set. We need to use aggregate functions on columns after SELECT
statement to make sense of the resulting set whenever we are using GROUP
BY.
SELECT ROLL_NO, ADDRESS, SUM(AGE) FROM STUDENT
GROUP BY (ADDRESS);
NOTE:
An attribute which is not a part of GROUP BY clause can’t be used for
selection.
Any attribute which is part of GROUP BY CLAUSE can be used for
selection but it is not mandatory.
But we could use attributes which are not a part of the GROUP BY clause
in an aggregate function.
Conclusion
SQL is a standard language used to manage data in relational databases. SQL
is mainly divided into four main categories: Data definition language, data
manipulation language, transaction control language, and data query language
and SQL is a powerful language that can be used to carry out a wide range of
operations like insert, delete and update.
Example:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);
In this example, a new table called employees is created with columns for
employee ID, first name, last name, and hire date.
2. DQL – Data Query Language
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
Example:
SELECT first_name, last_name, hire_date
FROM employees
WHERE department = 'Sales'
ORDER BY hire_date DESC;
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.
3. DML – Data Manipulation Language
The SQL commands that deal with the manipulation of data present in the
database belong to DML or Data Manipulation Language and this includes
most of the SQL statements. It is the component of the SQL statement that
controls access to data and to the database. Basically, DCL statements are
grouped with DML statements.
Common DML Commands
Command Description Syntax
Update existing
UPDATE table_name SET column1 = value1,
UPDATE data within a column2 = value2 WHERE condition;
table
Delete records
DELETE from a database DELETE FROM table_name WHERE condition;
table
Table control
LOCK LOCK TABLE table_name IN lock_mode;
concurrency
Call a PL/SQL or
CALL JAVA CALL procedure_name(arguments);
subprogram
Command Description Syntax
Describe the
EXPLAIN EXPLAIN PLAN FOR SELECT * FROM
access path to table_name;
PLAN
data
Example:
INSERT INTO employees (first_name, last_name, department)
VALUES ('Jane', 'Smith', 'HR');
This query inserts a new record into the employees table with the first name
‘Jane’, last name ‘Smith’, and department ‘HR’.
4. DCL – Data Control Language
DCL (Data Control Language) includes commands such
as GRANT and REVOKE which mainly deal with the rights, permissions, and
other controls of the database system. These commands are used to control
access to data in the database by granting or revoking permissions.
Common DCL Commands
Command Description Syntax
Removes previously
granted privileges from REVOKE [GRANT OPTION FOR]
a user account, taking privilege_type
REVOKE [(column_list)] ON
away their access to [object_type] object_name
certain database objects FROM user [CASCADE];
or actions.
Example of DCL
GRANT SELECT, UPDATE ON employees TO user_name;
This command grants the user user_name the permissions to select and
update records in the employees table.
5. TCL – Transaction Control Language
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. We can explore more
about transactions here.
Common TCL Commands
Command Description Syntax
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.
Most Important SQL Commands
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
TRUNCATE Removes all rows from a table but keeps its structure
TABLE intact.
IN / BETWEEN /
Used for advanced filtering conditions.
LIKE
Conclusion
SQL commands such as DDL, DML, DCL, DQL, and TCL are foundational for
effective database management. From creating and modifying tables
with DDL commands to managing transactions with TCL commands in SQL,
understanding each type of command enhances our database skills. Whether
we are manipulating data, or managing data, SQL provides all sets of tools.
Now, with this detailed guide, we hope you have gained a deep understanding
of SQL commands, their categories, and syntax with examples.
Joins in SQL
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are fundamental tools for combining data from multiple tables in
relational databases. Joins allow efficient data retrieval, which is essential for
generating meaningful observations and solving complex business queries.
Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT
JOIN, FULL JOIN, and NATURAL JOIN, is critical for working with relational
databases.
In this article, we will cover the different types of SQL joins, including INNER
JOIN, LEFT OUTER JOIN, RIGHT JOIN, FULL JOIN, and NATURAL JOIN. Each
join type will be explained with examples, syntax, and practical use cases to
help us understand when and how to use these joins effectively.
What is SQL Join?
An SQL JOIN clause is used to query and access data from multiple tables by
establishing logical relationships between them. It can access data from
multiple tables simultaneously using common key values shared across
different tables. We can use SQL JOIN with multiple tables. It can also be
paired with other clauses, the most popular use will be using JOIN with WHERE
clause to filter data retrieval.
Before diving into the specifics, let’s visualize how each join type operates:
INNER JOIN: Returns only the rows where there is a match in both
tables.
LEFT JOIN (LEFT OUTER JOIN): Returns all rows from the left table,
and the matched rows from the right table. If there’s no match, NULL
values are returned for columns from the right table.
RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right
table, and the matched rows from the left table. If there’s no match,
NULL values are returned for columns from the left table.
FULL JOIN (FULL OUTER JOIN): Returns all rows when there is a
match in one of the tables. If there’s no match, NULL values are
returned for columns from the table without a match.
1. SQL INNER JOIN
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
SELECT table1.column1,table1.column2,table2.column1,…. FROM table1
INNER JOIN table2 ON table1.matching_column = table2.matching_column;
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER
JOIN.
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
2. SQL LEFT JOIN
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.
HARSH 1
PRATIK 2
NAME COURSE_ID
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NULL 4
1 Ram 10
Employee
2 Jon 30
3 Bob 50
Department
Dept_id Dept_name
10 IT
30 HR
40 TIS
1 Ram 10 10 IT
2 Jon 30 30 HR
Conclusion
SQL joins are essential tools for anyone working with relational databases.
Understanding the different types of joins in SQL, like INNER JOIN, LEFT
OUTER JOIN, RIGHT JOIN, and FULL JOIN, allows us to combine and query
data effectively. With the examples and syntax covered here, we should feel
confident applying these SQL join types to our data to retrieve meaningful
observations and manage complex queries with ease. Use these SQL join
techniques to streamline our data handling and enhance our SQL skills.
StudentCourse Table
CourseID EnrollNo
1 1001
2 1001
3 1001
1 1002
CourseID EnrollNo
2 1003
Inner Join
Syntax:
SELECT COLUMN1, COLUMN2 FROM
[TABLE 1] INNER JOIN [TABLE 2]
ON Condition;
The following is a join query that shows the names of students enrolled in
different courses.
Query:
SELECT StudentCourse.CourseID,Student.StudentName
FROM Student
INNER JOIN StudentCourse
ON StudentCourse.EnrollNo = Student.EnrollNo
ORDER BY StudentCourse.CourseID;
Note: Inner is optional above. Simple JOIN is also considered as INNER JOIN
The above query would produce the following result.
CourseID StudentName
1 geek1
CourseID StudentName
1 geek2
2 geek1
2 geek3
3 geek1
Example:
For example, let’s say we have two tables, Table1 and Table2, with the
following data:
Table 1
ID Name
1 John
2 Sarah
3 David
Table 2
ID Address
If we perform an Inner Join on these tables using the ID column, the result set
would only include the matching rows from both tables, which are the rows
with ID values of 1 and 2:
Query:
SELECT Table1.ID,
Table 1. Name, Table 2.Address
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.ID
Output:
ID Name Address
Syntax:
SELECT T1.C1, T2.C2
FROM TABLE T1
LEFT JOIN TABLE T2
ON T1.C1= T2.C1;
Query:
SELECT Student.StudentName,StudentCourse.CourseID
FROM Student
LEFT OUTER JOIN StudentCourse
ON StudentCourse.EnrollNo = Student.EnrollNo
ORDER BY StudentCourse.CourseID;
Note: OUTER is optional above. Simple LEFT JOIN is also considered as LEFT
OUTER JOIN
StudentName CourseID
geek4 NULL
geek2 1
geek1 1
geek1 2
geek3 2
geek1 3
Syntax:
SELECT T1.C1, T2.C2
FROM TABLE T1
RIGHT JOIN TABLE T2
ON T1.C1= T2.C1;
Example:
Table Record
Roll_Number Name Age
1 Harsh 18
Roll_Number Name Age
2 Ankesh 19
3 Rupesh 18
4 Vaibhav 15
5 Naveen 13
6 Shubham 15
7 Ankit 19
8 Bhupesh 18
Table Course
Course_ID Roll_Number
1 1
2 2
2 3
3 4
1 5
4 9
5 10
4 11
Query:
SELECT Record.NAME,Course.COURSE_ID
FROM Record
RIGHT JOIN Course
ON Course.Roll_Number = Record.Roll_Number;
Output:
Name Course_ID
Harsh 1
Ankesh 2
Rupesh 2
Vaibhav 3
Naveen 1
NULL 4
NULL 5
NULL 4
Syntax:
SELECT * FROM T1
CROSS-JOIN T2;
Query:
SELECT Record.NAME,Course.COURSE_ID
FROM Record
FULL JOIN Course
ON Course.Roll_Number = Record.Roll_Number;
Output:
Name Course_ID
Harsh 1
Ankesh 2
Rupesh 2
Vaibhav 3
Naveen 1
Shubham NULL
Ankit NULL
Bhupesh NULL
NULL 4
NULL 5
NULL 4
Only the common data All data from one or both tables, with
Example between one or more giving NULLs for non-matching rows
Result tables. or the unmatched ones.
Conclusion
In SQL, The selection between Inner vs Outer is what are requirements of our
scenario including the data we are using in. A inner join is used when we need
to use filtering data which have matching values from both tables, ensuring
related data must be come up as result. Whereas, Outer Join is used when we
need to retain all records from both the tables without considering the matching
records overheads. Generally, Inner Joins are faster than the Outer Join as they
only retrieve the matching data. Understanding the Inner Join and Outer Join
makes a SQL data administrator a robust database manager to retain and
retrieve data effectively and quickly.
Having Vs Where Clause
Having vs Where Clause in SQL
In SQL, filtering data is important for extracting meaningful insights from large
datasets. While both the WHERE and HAVING clauses allow us to filter data,
they serve distinct purposes and operate at different stages of the query
execution process. Understanding the difference between these clauses is key
to writing efficient SQL queries.
In this article, we will explain the fundamental difference between
the WHERE and HAVING clauses, provides practical examples, and discuss
when to use each clause. By the end of this article, we will have a clear
understanding of how to use WHERE and HAVING for more effective data
filtering in SQL.
Difference Between HAVING and WHERE Clause in SQL
Here’s a table highlighting the key differences between
the WHERE and HAVING clauses in SQL:
Criteria WHERE Clause HAVING Clause
Stage of
Filters data
Query Filters data after GROUP BY.
before GROUP BY.
Execution
Generally more
Can be less efficient, as it filters after
Efficiency efficient, as it filters
aggregation.
data earlier.
Criteria WHERE Clause HAVING Clause
Example HAVING
WHERE column_name
aggregate_function(column_name) >
Usage = value
value
Order in Appears
Appears after GROUP BY.
Query before GROUP BY.
To select students with a score greater than or equal to 40, we can use
the WHERE clause. This query filters rows before any aggregation, selecting
only students with scores greater than or equal to 40.
Query:
SELECT Student, Score
FROM Marks
WHERE Score >= 40;
Output
Student Score
a 40
a 50
b 60
d 70
e 80
a 90
e 80
Explanation:
Here, we are using HAVING to filter groups of students after summing their
scores. This is because HAVING works on aggregated data,
whereas WHERE cannot be used for filtering after aggregation.
Conclusion
The WHERE and HAVING clauses in SQL are both used for filtering data, but
they work at different stages in the query execution process. WHERE filters
individual rows before aggregation, while HAVING filters groups after
aggregation. Understanding when to use each clause is essential for writing
effective and efficient SQL queries. By
using WHERE and HAVING appropriately, we can refine our data retrieval
process and gain more precise insights from our database.
Database Objects
Database Objects in DBMS
A database object is any defined object in a database that is used to store or
reference data.Anything which we make from create command is known as
Database Object.It can be used to hold and manipulate the data.Some of the
examples of database objects are : view, sequence, indexes, etc.
Table – Basic unit of storage; composed rows and columns
View – Logically represents subsets of data from one or more tables
Sequence – Generates primary key values
Index – Improves the performance of some queries
Synonym – Alternative name for an object
Different database Objects :
1. Table – This database object is used to create a table in database.
Syntax :
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);
Example :
CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(14),
loc VARCHAR2(13));
Output :
DESCRIBE dept;
Example :
CREATE VIEW salvu50
AS SELECT employee_id ID_NUMBER, last_name NAME,
salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;
Output :
SELECT *
FROM salvu50;
Example :
CREATE SEQUENCE dept_deptid_seq
INCREMENT BY 10
START WITH 120
MAXVALUE 9999
NOCACHE
NOCYCLE;
Example :
CREATE SYNONYM d_sum FOR dept_sum_vu;
STUDENT Table
2. COURSE Table
The STUDENT_COURSE table maps students to the courses they have
enrolled in. It uses the student and course IDs as foreign keys.
COURSE Table
3. STUDENT_COURSE Table
This table maps students to the courses they have enrolled in, with columns
for student ID (S_ID) and course ID (C_ID):
Student_Course Table
C1
C3
S1
S2
S4
Explanation: In this example, the inner query retrieves the C_IDs of the courses
‘DSA’ and ‘DBMS’, and the outer query retrieves the student IDs ( S_IDs)
enrolled in those courses.
Correlated Nested Queries
In correlated nested queries, the inner query depends on the outer query for its
execution. For each row processed by the outer query, the inner query is
executed. This means the inner query references columns from the outer query.
The EXISTS keyword is often used with correlated queries.
Example 2: Using EXISTS
In this Example, we will find the names of students who are enrolled in the
course with C_ID = ‘C1’:
SELECT S_NAME FROM STUDENT S
WHERE EXISTS (
SELECT 1 FROM STUDENT_COURSE SC
WHERE S.S_ID = SC.S_ID AND SC.C_ID = 'C1'
);
Output
S_NAME
RAM
RAMESH
Explanation:
For each student in the STUDENT table, the inner query checks if an entry
exists in the STUDENT_COURSE table with the same S_ID and the
specified C_ID. If such a record exists, the student’s name is included in the
output.
Common SQL Operators for Nested Queries
SQL provides several operators that can be used with nested queries to filter,
compare, and perform conditional checks.
1. IN Operator
The IN operator is used to check whether a column value matches any value in
a list of values returned by a subquery. This operator simplifies queries by
avoiding the need for multiple OR conditions.
Example: Retrieve student names who enrolled in ‘DSA’ or ‘DBMS’:
This query filters the students enrolled in the specified courses by chaining
multiple nested queries.
SELECT S_NAME FROM STUDENT
WHERE S_ID IN (
SELECT S_ID FROM STUDENT_COURSE
WHERE C_ID IN (
SELECT C_ID FROM COURSE WHERE C_NAME IN ('DSA', 'DBMS')
)
);
2. NOT IN Operator
The NOT IN operator excludes rows based on a set of values from a subquery.
It is particularly useful for filtering out unwanted results. This operator helps
identify records that do not match the conditions defined in the subquery.
Example: Retrieve student IDs not enrolled in ‘DSA’ or ‘DBMS’:
This query excludes students who are enrolled in the courses ‘DSA’ or
‘DBMS’.
SELECT S_ID FROM STUDENT
WHERE S_ID NOT IN (
SELECT S_ID FROM STUDENT_COURSE
WHERE C_ID IN (
SELECT C_ID FROM COURSE WHERE C_NAME IN ('DSA', 'DBMS')
)
);
Output
S_ID
S3
3. EXISTS Operator
The EXISTS operator checks for the existence of rows in a subquery. It returns
true if the subquery produces any rows, making it efficient for conditional
checks. This operator is often used to test for relationships between tables.
Example: Find student names enrolled in ‘DSA’
The inner query checks for matching records in the STUDENT_COURSE table,
and the outer query returns the corresponding student names.
SELECT S_NAME FROM STUDENT S
WHERE EXISTS (
SELECT 1 FROM STUDENT_COURSE SC
WHERE S.S_ID = SC.S_ID AND SC.C_ID = 'C1'
);
4. ANY and ALL Operators
ANY: Compares a value with any value returned by the subquery.
ALL: Compares a value with all values returned by the subquery.
Example using ANY:
SELECT S_NAME FROM STUDENT
WHERE S_AGE > ANY (
SELECT S_AGE FROM STUDENT WHERE S_ADDRESS = 'DELHI'
);
Example using ALL:
SELECT S_NAME FROM STUDENT
WHERE S_AGE > ALL (
SELECT S_AGE FROM STUDENT WHERE S_ADDRESS = 'DELHI'
);
Why Use Nested Queries?
Nested queries are highly useful when:
You need to retrieve data based on the results of another query.
You want to perform filtering or aggregation without using joins.
You need to break down complex operations into smaller steps.
Advantages of Nested Queries
Simplifies complex queries: Nested queries allow us to divide
complicated SQL tasks into smaller, more manageable parts. This
modular approach makes queries easier to write, debug, and
maintain.
Enhances flexibility: By enabling the use of results from one query
within another, nested queries allow dynamic filtering and indirect
joins, which can simplify query logic.
Supports advanced analysis: Nested queries empower developers to
perform operations like conditional aggregation, subsetting, and
customized calculations, making them ideal for sophisticated data
analysis tasks.
Improves readability: When properly written, nested queries can
make complex operations more intuitive by encapsulating logic
within inner queries.
Best Practices for Using Nested Queries
1. Optimize independent queries: Use independent nested queries
whenever possible, as they are easier to execute and debug. Ensure
that inner queries are optimized for performance by adding
appropriate indexes.
2. Prefer joins for simple queries: For cases where nested queries add
unnecessary complexity, consider using joins instead. Joins are
typically faster and more readable for straightforward relationships.
3. Avoid deep nesting: Limit the levels of nesting in queries to improve
performance and maintain readability. Deeply nested queries can be
computationally expensive and difficult to debug.
4. Use EXISTS and IN wisely: Choose the appropriate operator based
on the scenario. Use EXISTS for checking existence and IN for
comparing with a list of values.
5. Test query performance: Analyze the execution plan of our queries
to identify bottlenecks and improve efficiency. Rewrite queries where
necessary to enhance performance.
6. Maintain clarity: Use meaningful table aliases and comments to
ensure that nested queries remain understandable to other
developers or your future self.
Conclusion
Nested queries in SQL are a flexible feature that simplifies retrieving and
analyzing complex datasets. By understanding and applying the different
types of nested queries, such as independent and correlated subqueries, we
can optimize our database operations and streamline data management.
Nested queries are not only powerful but also essential for writing efficient and
effective SQL code. Use this article as our go-to resource for mastering SQL
nested queries and improving our database querying skills.
Join operation and nested queries are both used in relational database
management systems (RDBMS) to combine data from multiple tables, but
they differ in their approach.
Join operation:
A join operation combines data from two or more tables based on a common
column or columns. The join operation is performed using the JOIN keyword in
SQL, and it returns a single result set that contains columns from all the
tables involved in the join.
For example, let’s say we have two tables, Table1 and Table2, with the
following data:
Table1
ID | Name
1 | John
2 | Sarah
3 | David
Table2
ID | Address
1 | 123 Main St.
2 | 456 Elm St.
4 | 789 Oak St.
If we want to combine the data from these two tables based on the ID
column, we can perform an inner join using the following SQL query:
SELECT Table1.ID, Table1.Name, Table2.Address
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.ID
Result:
ID | Name | Address
1 | John | 123 Main St.
2 | Sarah | 456 Elm St.
Nested query:
A nested query is a SQL query that is embedded within another SQL query. It
is used to retrieve data from one or more tables based on a condition that is
evaluated using the results of another query. The nested query is executed
first, and its results are used to evaluate the outer query.
For example, let’s say we have the same two tables as before:
Table1
ID | Name
1 | John
2 | Sarah
3 | David
Table2
ID | Address
1 | 123 Main St.
2 | 456 Elm St.
4 | 789 Oak St.
If we want to retrieve the names of the people who have an address in
Table2, we can use a nested query as follows:
SELECT Name
FROM Table1
WHERE ID IN (SELECT ID FROM Table2)
Result:
Name
John
Sarah
In this case, the nested query is executed first, and it returns the ID values of
the rows in Table2. These ID values are then used to evaluate the outer query,
which retrieves the names of the people who have those ID values in Table1.
The choice between using a join operation or a nested query depends on the
specific requirements of the task at hand. Joins are often faster and more
efficient for large datasets, but nested queries can be more flexible and allow
for more complex conditions to be evaluated.
In general, there are two types of file organization mechanisms that are
followed by the indexing methods to store the data:
Sequential File Organization or Ordered Index File
In this, the indices are based on a sorted ordering of the values. These are
generally fast and a more traditional type of storing mechanism. These Ordered
or Sequential file organizations might store the data in a dense or sparse format.
Dense Index
o For every search key value in the data file, there is an
index record.
o This record contains the search key and also a
reference to the first data record with that search key
value.
Dense Index
Sparse Index
o The index record appears only for a few items in the
data file. Each item points to a block as shown.
o To locate a record, we find the index record with the
largest search key value less than or equal to the
search key value we are looking for.
o We start at that record pointed to by the index record,
and proceed along with the pointers in the file (that is,
sequentially) until we find the desired record.
o Number of Accesses required=log₂(n)+1, (here
n=number of blocks acquired by index file)
Sparse Index
Multilevel Indexing
Advantages of Indexing
Improved Query Performance: Indexing enables faster data retrieval
from the database. The database may rapidly discover rows that
match a specific value or collection of values by generating an index
on a column, minimizing the amount of time it takes to perform a
query.
Efficient Data Access: Indexing can enhance data access efficiency
by lowering the amount of disk I/O required to retrieve data. The
database can maintain the data pages for frequently visited columns
in memory by generating an index on those columns, decreasing the
requirement to read from disk.
Optimized Data Sorting: Indexing can also improve the performance
of sorting operations. By creating an index on the columns used for
sorting, the database can avoid sorting the entire table and instead
sort only the relevant rows.
Consistent Data Performance: Indexing can assist ensure that the
database performs consistently even as the amount of data in the
database rises. Without indexing, queries may take longer to run as
the number of rows in the table grows, while indexing maintains a
roughly consistent speed.
By ensuring that only unique values are inserted into columns that
have been indexed as unique, indexing can also be utilized to ensure
the integrity of data. This avoids storing duplicate data in the
database, which might lead to issues when performing queries or
reports.
Overall, indexing in databases provides significant benefits for improving query
performance, efficient data access, optimized data sorting, consistent data
performance, and enforced data integrity
Disadvantages of Indexing
Indexing necessitates more storage space to hold the index data
structure, which might increase the total size of the database.
Increased database maintenance overhead: Indexes must be
maintained as data is added, destroyed, or modified in the table,
which might raise database maintenance overhead.
Indexing can reduce insert and update performance since the index
data structure must be updated each time data is modified.
Choosing an index can be difficult: It can be challenging to choose
the right indexes for a specific query or application and may call for a
detailed examination of the data and access patterns.
Features of Indexing
The development of data structures, such as B-trees or hash tables,
that provide quick access to certain data items is known as indexing.
The data structures themselves are built on the values of the indexed
columns, which are utilized to quickly find the data objects.
The most important columns for indexing columns are selected
based on how frequently they are used and the sorts of queries they
are subjected to. The cardinality, selectivity, and uniqueness of the
indexing columns can be taken into account.
There are several different index types used by databases, including
primary, secondary, clustered, and non-clustered indexes. Based on
the particular needs of the database system, each form of index
offers benefits and drawbacks.
For the database system to function at its best, periodic index
maintenance is required. According to changes in the data and usage
patterns, maintenance work involves building, updating, and
removing indexes.
Database query optimization involves indexing, which is essential.
The query optimizer utilizes the indexes to choose the best execution
strategy for a particular query based on the cost of accessing the
data and the selectivity of the indexing columns.
Databases make use of a range of indexing strategies, including
covering indexes, index-only scans, and partial indexes. These
techniques maximize the utilization of indexes for particular types of
queries and data access.
When non-contiguous data blocks are stored in an index, it can result
in index fragmentation, which makes the index less effective. Regular
index maintenance, such as defragmentation and reorganization, can
decrease fragmentation.
Conclusion
Indexing is a very useful technique that helps in optimizing the search time
in database queries. The table of database indexing consists of a search key
and pointer. There are four types of indexing: Primary, Secondary Clustering,
and Multivalued Indexing. Primary indexing is divided into two types, dense and
sparse. Dense indexing is used when the index table contains records for every
search key. Sparse indexing is used when the index table does not use a search
key for every record. Multilevel indexing uses B+ Tree. The main purpose of
indexing is to provide better performance for data retrieval.
SQL-Clauses
SQL Views
iews in SQL are a type of virtual table that simplifies how users interact with
data across one or more tables. Unlike traditional tables, a view in SQL does
not store data on disk; instead, it dynamically retrieves data based on a pre-
defined query each time it’s accessed.
SQL views are particularly useful for managing complex queries, enhancing
security, and presenting data in a simplified format. In this guide, we will cover
the SQL create view statement, updating and deleting views, and using the
WITH CHECK OPTION clause.
What is a View in SQL?
A view in SQL is a saved SQL query that acts as a virtual table. Unlike regular
tables, views do not store data themselves. Instead, they dynamically
generate data by executing the SQL query defined in the view each time it is
accessed. It can fetch data from one or more tables and present it in
a customized format, allowing developers to:
Simplify Complex Queries: Encapsulate complex joins and
conditions into a single object.
Enhance Security: Restrict access to specific columns or rows.
Present Data Flexibly: Provide tailored data views for different
users.
We will be using these two SQL tables for examples.
StudentDetails
-- Create StudentDetails table
CREATE TABLE StudentDetails (
S_ID INT PRIMARY KEY,
NAME VARCHAR(255),
ADDRESS VARCHAR(255)
);
StudentMarks
-- Create StudentMarks table
CREATE TABLE StudentMarks (
ID INT PRIMARY KEY,
NAME VARCHAR(255),
Marks INT,
Age INT
);
OR
SQL Indexes
When it comes to relational database performance optimizing, SQL indexes are
a game changer. Imagine them like an index in a book instead of flipping
through every page (row), the database can jump right to the data it requires.
SQL Indexes are crucial elements in relational databases that greatly
improve data retrieval speeds by minimizing the need for full table scans. By
providing quick access paths, indexes allow queries to perform faster,
especially on large datasets. However, while indexes speed up SELECT
queries, they can slow down data manipulation operations (INSERT, UPDATE,
DELETE)
What Are Indexes in SQL?
An index in SQL is a schema object that improves the speed of data
retrieval operations on a table. It works by creating a separate data
structure that provides pointers to the rows in a table, making it faster to look
up rows based on specific column values. Indexes act as a table of contents for
a database, allowing the server to locate data quickly and efficiently, reducing
disk I/O operations.
Benefits of Indexes:
Faster Queries: Speeds up SELECT and JOIN operations.
Lower Disk I/O: Reduces the load on your database by limiting the
amount of data scanned.
Better Performance on Large Tables: Essential when working with
millions of records.
Creating an Index
Creating an index allows us to define a quick access path to data. SQL
indexes can be applied to one or more columns and can be
either unique or non-unique. Unique indexes ensure that no duplicate
values are entered in the indexed columns, while non-unique indexes simply
speed up queries without enforcing uniqueness. You can create:
Single-column indexes: For basic queries
Multi-column indexes: For queries using multiple filters
Unique indexes: To ensure data uniqueness
Syntax:
CREATE INDEX index
ON TABLE column;
Example
CREATE INDEX idx_product_id
ON Sales (product_id);
Explanation:
This creates an index named idx_product_id on the product_id column in
the Sales table, improving the speed of queries that filter or join based on this
column.
Multi – Column Indexes
If queries often use more than one column in conditions, we can create a multi-
column index for better performance.
Syntax:
CREATE INDEX index
ON TABLE (column1, column2,…..);
Example
CREATE INDEX idx_product_quantity
ON Sales (product_id, quantity);
Explanation:
This index allows the database to quickly filter or join data based on
both product_id and quantity columns.
Unique Indexes
A unique index ensures that all values in the indexed column(s) are unique,
preventing duplicates. These are useful for maintaining the integrity of the data,
ensuring that no two rows have the same values in the indexed columns.
Syntax:
CREATE UNIQUE INDEX index_name
ON table_name (column_name);
Example
CREATE UNIQUE INDEX idx_unique_employee_id
ON Employees (employee_id);
Explanation:
This index ensures that no two rows in the Employees table have the
same employee_id, which maintains data integrity and prevents duplicate
entries.
Removing an Index
If an index is no longer needed, it can be removed to improve write
performance or save storage space. As indexes can slow down
operations like INSERT, UPDATE, and DELETE due to the overhead of
maintaining them, dropping unnecessary indexes can improve overall
database efficiency. The DROP INDEX command is used for this purpose.
Syntax
DROP INDEX index;
Explanation:
This command removes an index from the database schema. It does not affect
the underlying data in the table but may slow down future queries that would
have benefited from the index.
Altering an Index
If an index requires adjustments, such as reorganizing or rebuilding, it can be
altered without affecting the data. This is useful for optimizing index
performance as tables grow larger.
Syntax:
ALTER INDEX IndexName
ON TableName REBUILD;
Explanation:
This command rebuilds the specified index, which can optimize query
performance by reorganizing its structure, especially in large tables.
Confirming and Viewing Indexes
We can view all the indexes in a database to understand which ones are in use
and confirm their structure. In SQL, the following query helps us see the indexes
for a given table:
Syntax:
SELECT * from USER_INDEXES;
Explanation:
This query retrieves all the indexes in the database schema, showing their
names and the columns they are associated with. We can use this information
to audit or manage existing indexes.
Renaming an Index
In some cases, renaming an index might be necessary for clarity or consistency.
While SQL doesn’t directly support renaming indexes, we can use a
combination of commands to achieve this.
Syntax:
EXEC sp_rename ‘old_index_name’, ‘new_index_name’, ‘INDEX’;
Explanation:
This command allows us to rename an existing index, which helps maintain
clarity in our database schema.
When Should Indexes Be Created?
Indexes can significantly improve query performance, but they should be used
judiciously. The following scenarios warrant creating indexes:
1. Wide Range of Values: Indexes are helpful when a column has a
wide range of values, such as product IDs or customer names, as they
speed up search operations.
2. Non-NULL Values: Columns that don’t contain many NULL values
are ideal candidates for indexing, as NULLs complicate the indexing
process.
3. Frequent Query Conditions: Indexes should be created on columns
frequently used in WHERE clauses or as part of a join condition.
When Should Indexes Be Avoided?
While indexes enhance performance, they may not always be beneficial,
especially in certain situations:
1. Small Tables: Indexes are not needed for small tables as queries will
likely perform well without them.
2. Infrequent Query Use: If a column is rarely used in queries, indexing
it will only add overhead.
3. Frequently Updated Columns: Avoid indexing columns that are
frequently updated, as the index will need to be updated with each
change, adding overhead.
Why SQL Indexing is Important?
Indexing in SQL is a critical feature for optimizing query performance,
especially for large datasets. Here are some common scenarios where indexing
proves beneficial:
1. Large Data Tables: SQL queries on tables with millions of rows can
significantly slow down due to full table scans. Indexes provide a
faster alternative by allowing quick access to relevant rows.
2. Join Optimization: Indexes on columns used for joining tables (such
as foreign keys) improve the performance of complex joins.
3. Search Operations: Queries that search for specific values in a
column can be sped up with indexes, reducing the time required to
perform lookups.
4. However, it is essential to be mindful of the storage cost and
performance tradeoffs associated with indexes. Over-indexing can
lead to unnecessary overhead, while under-indexing may slow down
data retrieval
Conclusion
SQL Indexing is important in database optimization as it accelerates data
retrieval, query performance, and database management as a
whole. Knowing when and how to create, maintain, and drop indexes can
help database administrators optimize application performance
while ensuring an efficient and scalable database schema.
Always make sure that indexes are created wisely based on
query requirements and data structure to achieve the correct balance between
read and write performance.
SQL Queries on Clustered and Non-Clustered
Indexes
ndexes in SQL play a pivotal role in enhancing database performance by
enabling efficient data retrieval without scanning the entire table. The two
primary types of indexes Clustered Index and Non-Clustered Index serve
distinct purposes in optimizing query performance.
In this article, we will explain Clustered and Non-Clustered Indexes in detail,
their creation, use cases, and examples to help us master the concepts.
What are Indexes in SQL?
Indexing in SQL is similar to the index page in a book, they allow the database
to quickly locate data without scanning the entire table. Without indexing, SQL
Server performs a full table scan, which can be time-consuming for large
datasets. By creating indexes, SQL Server optimizes query execution, reducing
retrieval time. In the same way, a table’s index allows us to locate the exact
data without scanning the whole table.
Key Benefits of Indexing
Faster SELECT queries.
Efficient data access for UPDATE, DELETE, and JOIN operations.
Minimizes disk I/O operations.
Types of Indexes in SQL
1. Clustered index
2. Non-clustered index
Clustered Index
A clustered index is the type of indexing that establishes a physical sorting
order of rows. The data rows are stored directly in the order of the indexed
column(s). Each table can have only one clustered index because it dictates
the data’s physical storage. A clustered index is like a Dictionary in which the
sorting order is alphabetical and there is no separate index page.
Suppose we have a table Student_info which contains ROLL_NO as a primary
key, then the clustered index which is self-created on that primary key will sort
the Student_info table as per ROLL_NO.
Example: Creating a Clustered Index
CREATE TABLE Student_info
(
ROLL_NO int(10) primary key,
NAME varchar(20),
DEPARTMENT varchar(20),
);
INSERT INTO Student_info values(1410110405, 'H Agarwal', 'CSE');
INSERT INTO Student_info values(1410110404, 'S Samadder', 'CSE');
INSERT INTO Student_info values(1410110403, 'MD Irfan', 'CSE');
Explanation:
The ROLL_NO column is the primary key, making it the Clustered
Index by default.
Rows are stored in ascending order of ROLL_NO.
Syntax
create NonClustered index IX_table_name_column_name on table_name
(column_name ASC)
We will create a Non-Clustered Index on the NAME column to improve query
performance when searching by name. Here is the SQL Query for the same
Query:
create NonClustered index IX_Student_info_NAME on Student_info (NAME
ASC)
Output
NAME ROW_ADDRESS
H Agarwal 1
MD Irfan 3
S Samadder 2
Data Storage Determines the physical order Does not affect the physical
Order of data in the table. order of data.
If no clustered index is
Primary Key specified, the primary key Can be created on any column,
Default usually becomes the clustered not necessarily a primary key.
index.
Non-Clustered Index
If a Non-Clustered Index is created on the NAME column, the query optimizer
uses the index to locate matching rows efficiently.
Example
-- Create a non-clustered index on NAME
CREATE NONCLUSTERED INDEX IX_Student_info_NAME
ON Student_info (NAME ASC);
2. UPDATE Queries
Clustered Index
When updating a row in a table with a Clustered Index, the database can
quickly locate the row to modify based on the indexed column.
Example:
-- Update the DEPARTMENT of a specific ROLL_NO
UPDATE Student_info
SET DEPARTMENT = 'ECE'
WHERE ROLL_NO = 1410110403;
Output
ROLL_NO NAME DEPARTMENT
Non-Clustered Index
If the UPDATE query references columns that are not part of the clustered
index, SQL Server may need to perform additional disk writes to update the
non-clustered index as well.
Example:
-- Update NAME for a specific ROLL_NO
UPDATE Student_info
SET NAME = 'Harsh Agarwal'
WHERE ROLL_NO = 1410110405;
Output
ROLL_NO NAME DEPARTMENT
3. JOIN Queries
Clustered Index
When performing a JOIN operation between two large tables, SQL Server can
use the clustered index on the join column(s) to efficiently match the rows from
both tables. This can significantly reduce the time required to complete the
query.
Example:
-- Create another table for join
CREATE TABLE Course_enrollments (
ROLL_NO INT,
COURSE_NAME VARCHAR(20)
);
-- Join query
SELECT s.ROLL_NO, s.NAME, e.COURSE_NAME
FROM Student_info s
JOIN Course_enrollments e
ON s.ROLL_NO = e.ROLL_NO;
Output
ROLL_NO NAME COURSE_NAME
Non-Clustered Index
If the JOIN operation references columns that are not part of the clustered
index, SQL Server can use a non-clustered index to find the matching rows.
However, this may require additional disk reads and slow down the query.
Conclusion
Both clustered and non-clustered indexes play important roles in
optimizing SQL query performance. While clustered indexes physically
reorder the table’s data for better range queries, non-clustered indexes offer
flexibility by providing fast lookups without altering data order. Understanding
when and how to implement these indexes is key to
improving database performance.
SQL Tutorial
SQL is a Structured query language used to access and manipulate data in
databases. SQL stands for Structured Query Language. We can create, update,
delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc.
Overall, SQL is a query language that communicates with databases.
In this SQL tutorial, you’ll learn all the basic to advanced SQL concepts
like SQL queries, SQL join, SQL injection, SQL insert, and creating tables in
SQL.
SQL Uses
SQL's integration with various technologies makes it essential for managing
and querying data in databases. Whether it's in traditional relational databases
(RDBMS) or modern technologies such as machine learning, AI, and blockchain,
SQL plays a key role. It works seamlessly with DBMS (Database Management
Systems) to help users interact with data, whether stored in
structured RDBMS or other types of databases.
Data Science & Analytics: Used for querying large datasets, data
cleaning, and analysis. Analysts use SQL to generate reports and
insights that inform business decisions.
Machine Learning & AI: Helps in preparing and managing the data
required for training machine learning models and AI algorithms. It is
used for data cleaning, transformation, and extraction.
Web Development: Used to manage user data, e-commerce
transactions, and content management in websites and applications
built with frameworks like Django, Node.js, and Ruby on Rails.
Cloud and Big Data: SQL is integrated into cloud-based databases
(e.g., Amazon RDS, Microsoft Azure SQL) and Big Data platforms
(e.g., Apache Hive) to enable seamless data querying and
management.
Blockchain and Decentralized Systems: In blockchain systems, SQL
can be used to manage off-chain data, providing efficient data
storage and retrieval alongside decentralized ledger technology.
How SQL work?
When you interact with a database, you typically use SQL commands to
perform these operations. These commands are translated into actions by the
SQL Engine, the core component responsible for processing queries. The SQL
Engine parses and compiles SQL queries, optimizing and executing them to
interact with the stored data. The SQL Engine also ensures that data retrieval
and modifications are efficient and consistent.
Different DBMS tools (like MySQL, SQL Server, etc.) provide an interface and
APIs that users can use to interact with the database. These tools provide a
user-friendly way to write and execute SQL queries, but internally, they rely on
their respective SQL Engines to process these commands.
For example, MySQL uses its own SQL Engine to parse, optimize, and execute
queries, while SQL Server has a different SQL Engine for the same task. These
engines ensure that SQL queries are executed in a way that respects the
underlying database structure and the specific DBMS’s optimizations.
Example
In this detailed SQL tutorial for beginners, we'll explore practical SQL
examples for managing employee data within a database. We'll create a table
to store employee information and populate it with sample data
like Employee_Id, Name, Age, Department, and Salary.
If you want to retrieves data from the employees table where the salary is
greater than 55000.00 then we will use SELECT Statement.
Query:
SELECT * FROM employees WHERE Salary > 55000.00;
Basics
SQL or Structured Query Language is a fundamental skill for anyone who wants
to interact with databases. This standard Query Language all users to create,
manage, and retrieve data from relational databases. In this SQL tutorial PDF,
we have listed all the basics of SQL. Explore this section to sharpen your SQL
basics.
Introduction
Data Types
Operators
Commands
Create Database
The first step to storing the information electronically using SQL includes
creating database. And in this section we will learn how to Create, Select, Drop,
and Rename databases with examples.
CREATE Database
DROP Database
RENAME Database
SELECT Database
Tables
The cornerstone of any SQL database is the table. Basically, these structure
functions is very similar to spreadsheets, which store data in very organized
grid format. In this section, you will learn how to Create, Drop, Delete, and more
related to Table.
CREATE TABLE
DROP TABLE
RENAME TABLE
TRUNCATE TABLE
COPY TABLE
TEMP TABLE
ALTER TABLE
CRUD Operations
In this section, you will learn about the SQL Queries like SELECT statement,
SELECT LAST, and more. Explore this section and learn how to use these
queries.
SELECT Statement
INSERT INTO
INSERT Multiple Rows
UPDATE Statement
DELETE Statement
DELETE Duplicate Rows
Clauses
Unlock the power of SQL Clauses with this SQL tutorial. Here in this section,
you will learn how to use SELECT, WHERE, JOIN, GROUP BY, and more to
query databases effectively.
WHERE Clause
WITH Clause
HAVING Clause
ORDER By Clause
Group By Clause
LIMIT Clause
Distinct Clause
FETCH
Aliases
Operators
SQL Operators" refers to the fundamental symbols and keywords within the
SQL that enable users to perform various operations and SQL AND, OR, LIKE,
NOT, and more operators on databases. Here, we have discussed all the SQL
operators in a detailed manner with examples.
AND Operator
OR Operator
Logical Operators
LIKE Operator
IN Operator
NOT Operator
NOT EQUAL Operator
IS NULL Operator
UNION Operator
UNION ALL Operator
EXCEPT Operator
BETWEEN Operator
ALL and ANY
INTERSECT Operator
EXISTS Operator
CASE Operator
Aggregate Functions
Whether you are calculating the total sales revenue for a particular product,
finding the average age of customers, or determining the highest value in a
dataset, SQL Aggregate Functions make these tasks straightforward and
manageable.
Aggregate Function
Count() Function
SUM() Function
MIN() Function
MAX() Function
AVG() Function
Data Constraints
Constraints act as rules or conditions imposed on the data, dictating what
values are permissible and what actions can be taken. They play a crucial role
in maintaining the quality and coherence of the database by preventing errors.
So, explore this section to get a hand on SQL Data Constraints.
NOT NULL Constraints
Primary Key Constraints
Foreign Key Constraints
Composite Key
Unique Constraints
Alternate Key
CHECK Constraints
DEFAULT Constraints
Joining Data
SQL joins serve as the weaver's tool, allowing you to seamlessly merge data
from multiple tables based on common threads. So explore this section to learn
how to use JOIN command.
JOIN
Outer Join
Left Join
Right Join
Full Join
Cross Join
Self Join
UPDATE with JOIN
DELETE JOIN
Recursive Join
Functions
SQL functions offer an efficient and versatile approach to data analysis. By
leveraging these functions within your queries, you can enhance the depth and
accuracy of your insights, transforming raw data into actionable knowledge.
Date Functions
String Functions
Numeric Functions
Statistical Functions
JSON Functions
Conversion Functions
Datatype Functions
LTRIM Function
UPPER Function
RTRIM Function
Views
Views makes easier for anyone to access the information they need, without
getting bogged down in complicated queries. Views also act like a helpful
security guard, keeping the most sensitive information in the back room, while
still allowing access to what's needed.
CREATE VIEW
UPDATE VIEW
RENAME VIEW
DELETE VIEW
Indexes
Indexes work by organizing specific columns in a particular order, allowing the
database to quickly pinpoint the information you need. And in this section, we
have listed all the points that one has to learn while learning SQL.
Indexes
Create Index
Drop Index
Show Indexes
Unique Index
Clustered Index vs Non-Clustered Index
Subquery
Subqueries allow you to perform nested queries within a larger query,
enabling more complex data retrieval. They help in filtering data or
performing operations on data that would otherwise require multiple queries.
Subquery
Correlated Subqueries
Nested Queries
Miscellaneous Topics
In this miscellaneous section, you will encounter concepts like stored
procedures for automating repetitive tasks, triggers for automated actions
based on data changes, and window functions for complex calculations within
a single query.
Wildcards Operators
Comments
Pivot and Unpivot
Trigger
Hosting
Performance Tuning
Stored Procedures
Transactions
Sub Queries
Using Sequences
Auto Increment
Window functions
Cursors
Common Table Expressions
Database Tuning
Dynamic
Regular Expressions
Exercises, Interview Questions & Cheat Sheet
This section provides hands-on exercises and commonly asked interview
questions to help solidify your SQL knowledge. It also includes a cheat sheet
for quick reference, making SQL concepts easier to grasp.
Exercises
Quiz
Interview Questions
Query Interview Questions
Cheat Sheet
30 Days of SQL – From Basic to Advanced
SQL Tutorial
Jobs and Opportunities
There are numerous companies around the globe seeking SQL professionals,
and they pay high packages. The average salary of SQL developers is
around 40,000–65,000 INR. In this section, we have listed some of the top giant
companies that hire SQL experts.
Google
Microsoft
Amazon
Meta
Apple
Accenture
Deloitte
McKinsey & Company
Boston Consulting Group
KPMG
JPMorgan Chase
Bank of America
Citigroup
Wells Fargo
HSBC
Netflix
Airbnb
Capgemini
Wipro
Infosys
Tata Consultancy
Advantages
SQL or Structured Query Language, is one of the most popular query
languages in the field of data science. SQL is the perfect query language that
allows data professionals and developers to communicate with their
databases.
In the below section, we have listed some of the most prominent advantages
or benefits of Structured Query Language:
Simple and Easy to Learn: Most of the commands and syntax in SQL
are like normal English, which makes SQL easy to learn. Along with
this, SQL follows a logical structure that helps promote readability
and understanding.
Efficiency and Speed Execution: Well, SQL is optimized for RDBMS,
which means relational database, and this thing ensures fast query
execution.
Standardization: SQL is a widely accepted standard query language
and ensures compatibility across different database systems.
Scalable: SQL can efficiently manage massive datasets,
accommodating growth without compromising performance.
Latest Trends and Updates
The world of SQL is constantly evolving, so here are some of the hottest
trends and updates to keep you in the loop:
Big Data and SQL: Big data store vast amounts of information from various
sources. SQL queries act as a bridge, enabling users to extract specific data
subsets for further analysis.
Cloud Computing and SQL: Cloud SQL lets your database scale up or down
based on your needs. Along with that it very cost effective so you have only pay
for the resources you use, making it a cost-efficient option for businesses of all
sizes.
Machine Learning and SQL: Data scientists leverage SQL to prepare and clean
data for analysis, making it a crucial skill for this field.
Real-time Data Processing with SQL: The need for immediate insights is
driving the growth of streaming SQL. This allows you to analyze data as it's
generated, providing real-time visibility into what's happening.
SQL in Data Governance and Compliance: With stricter data privacy
regulations, SQL is playing a role in ensuring data security and compliance.
Queries can be used to control access to sensitive information and track data
usage for auditing purposes.
MySQL | Common MySQL Queries
MySQL server is a open-source relational database management system
which is a major support for web based applications. Databases and related
tables are the main component of many websites and applications as the data
is stored and exchanged over the web. Even all social networking websites
mainly Facebook, Twitter, and Google depends on MySQL data which are
designed and optimized for such purpose. For all these reasons, MySQL server
becomes the default choice for web applications.
MySQL server is used for data operations like querying, sorting, filtering,
grouping, modifying and joining the tables. Before learning the commonly
used queries, let us look into some of the advantages of MySQL.
Advantages of MySQL :
Fast and high Performance database.
Easy to use, maintain and administer.
Easily available and maintain integrity of database.
Provides scalability, usability and reliability.
Low cost hardware.
MySQL can read simple and complex queries and write operations.
InnoDB is default and widely used storage engine.
Provides strong indexing support.
Provides SSL support for secured connections.
Provides powerful data encryption and accuracy.
Provides Cross-platform compatibility.
Provides minimized code repetition.
Queries can be understood as the commands which interacts with database
tables to work around with data. Some of the commonly used MySQL queries,
operators, and functions are as follows :
1. SHOW DATABASES
This displays information of all the existing databases in the server.
Output:
Note : The
databases ‘information_schema’, ‘mysql’ and ‘performance_schema’ are
system databases which are used internally by MySQL server.
A ‘test’ database is meant for testing purpose which is provided during
installation.
2. USE database_name
database_name : name of the database
This sets the database as the current database in the MySQL server.
To display the current database name which is set, use syntax
SELECT DATABASE();
3. DESCRIBE table_name
table_name : name of the table
This describes the columns of the table_name with respect to Field, Type,
Null, Key, Default, Extra.
4. SHOW TABLES
This shows all the tables in the selected database as a information.
5. SHOW CREATE TABLE table_name
table_name : name of the table
This shows the complete CREATE TABLE statement used by MySQL for
creating the table.
6. SELECT NOW()
MySQL queries mostly starts with SELECT statement.
This query shows the current date and time.
Output :
2019-09-24 07:08:30
7. SELECT 2 + 4;
Output :
6
This executes SELECT statement without any table.
SELECT can be used for executing an expression or evaluating an in-built
function.
SELECT can also be used for more than one or many columns.
Example :
SELECT 2+4, CURDATE();
Output :
8. Comments
Comments are of two types. Multi-line comments or single-line or end-of-line
comment.
/* These are multi-line comments. */
# This is single-line comment.
-- This is also single-line comment.
9. CREATE DATABASE database_name
database_name : name of the database
This statement creates a new database.
10. DROP DATABASE database_name
database_name : name of the database
This statement deletes the database.
Note : User has to be very careful before deleting a database as it will lose all
the crucial information stored in the database.
11. CREATE TABLE table_name(column1, column2, column3..)
table_name : name of the table
column1 : name of first column
column2 : name of second column
column3 : name of third column
When the developer start building an application, he needs to create database
tables.
This statement creates a new table with the given columns.
Example :
CREATE TABLE employee(
'id' INTEGER NOT NULL AUTO_INCREMENT,
'name' VARCHAR(30) NOT NULL,
'profile' VARCHAR(40) DEFAULT 'engineer',
PRIMARY KEY ('id')
)ENGINE = InnoDB;
Note : You have ‘id’ column as AUTO_INCREMENT with a primary key
constraint which ensures that each id is incremented value, avoiding
duplication. Storage engine selected is ‘InnoDB’ allowing foreign key
constraint and related transactions.
12. AUTO_INCREMENT
It is used to generate a unique identification field for new row.
13. DROP TABLE table_name
table_name : name of the table
This statement deletes the mentioned table.
14. RENAME TABLE old_table_name TO new_table_name
old_table_name : name of the previous table.
new_table_name : name of the new table.
This statement renames the table to a new name.
15. ALTER TABLE table_name ADD(column1, column2, column3..)
table_name : name of the existing table.
column1 : name of first column.
column2 : name of second column.
column3 : name of third column.
This statement adds columns to the existing table.
16. ALTER TABLE table_name DROP(column1)
table_name : name of the existing table.
column1 : name of first column.
This statement deletes specified columns from the existing table.
17. INSERT INTO table_name (column1, column2, column3 . . )
VALUES(value1, value2, value3 . . )
table_name : name of the existing table.
column1 : name of first column.
column2 : name of second column.
column3 : name of third column.
value1 : value for first column.
value2 : value for second column.
value3 : value for third column.
This statement inserts a new record into a table with specified values.
18. UPDATE table_name SET column1 = value1, column2 = value2, column3
= value3.. WHERE condition
table_name : name of the table.
column1 : name of first column.
column2 : name of second column.
column3 : name of third column.
value1 : value for first column.
value2 : value for second column.
value3 : value for third column.
condition : the condition statement.
This statement update records in the table with the new given values for the
columns.
Note : WHERE clause in MySQL queries is used to filter rows for a specific
condition.
19. DELETE FROM table_name WHERE condition
table_name : name of the table.
condition : the condition statement.
This statement deletes records from the table.
20. SELECT column1, column2, column3.. FROM table_name WHERE
condition
table_name : name of the table.
column1 : name of first column.
column2 : name of second column.
column3 : name of third column.
condition : the condition statement.
This statement executes and gives records from specific columns from the
table which matches the condition after WHERE clause.
21. SELECT * FROM table_name
table_name: name of the table.
Instead of specifying one column or many columns, you can use an asterisk (*)
which represents all columns of table. This query retrieves all records from
the table.
22. COUNT
The COUNT function is used to return total number of records matching a
condition from any table.
It is one of the known AGGREGATE function.
Example :
SELECT COUNT(*) from student;
Note: AGGREGATE functions allow you to run calculations on data and
provide information by using
a SELECT query.
23. MAX
It is used to get the maximum numeric value of a particular column of table.
Example :
SELECT MAX(marks) FROM student_report;
Note: The MIN and MAX functions work correctly on numeric as well as
alphabetic values.
24. MIN
It is used to get the minimum numeric value of a particular column of table.
Example :
SELECT MIN(marks) FROM student_report;
Note : The above given example queries can also be nested with each other
depending on the requirement.
Example :
SELECT MIN(marks)
FROM student_report
WHERE marks > ( SELECT MIN(marks) from student_report);
25. LIMIT
It is used to set the limit of number of records in result set.
Example :
SELECT *
FROM student limit 4, 10;
This gives 10 records starting from the 5th record.
26. BETWEEN
It is used to get records from the specified lower limit to upper limit.
This verifies if a value lies within that given range.
Example :
SELECT * FROM employee
WHERE age BETWEEN 25 AND 45.
27. DISTINCT
This is used to fetch all distinct records avoiding all duplicate ones.
Example :
SELECT DISTINCT profile
FROM employee;
28. IN clause
This verifies if a row is contained in a set of given values.
It is used instead of using so many OR clause in a query.
Example :
SELECT *
FROM employee
WHERE age IN(40, 50, 55);
29. AND
This condition in MySQL queries are used to filter the result data based on
AND conditions.
Example :
SELECT NAME, AGE
FROM student
WHERE marks > 95 AND grade = 7;
30. OR
This condition in MySQL queries are used to filter the result data based on OR
conditions.
Example :
SELECT *
FROM student
WHERE address = 'Hyderabad' OR address = 'Bangalore';
31. IS NULL
This keyword is used for boolean comparison or to check if the data value of a
column is null.
Example :
SELECT *
FROM employee
WHERE contact_number IS NULL;
32. FOREIGN KEY
It is used for pointing a PRIMARY KEY of another table.
Example :
CREATE TABLE Customers
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
)
SELECT *
FROM employee
WHERE name LIKE '%Sh%';
Note: Percentage signs (%) in the query represent zero or more characters.
34. JOINS
Joins are the joining of two or more database tables to fetch data based on a
common field.
There are various types of joins with different names in different databases.
Commonly known joins are self join, outer join, inner join and many more.
Regular Join :
It is the join which gets all the records from both the tables which exactly
match the given condition.
Example :
SELECT student.name, department.name
FROM student JOIN department ON student.department = department.name
Left Join :
It is the join which gets all the records that match the given condition, and
also fetch all the records from
the left table.
Example :
SELECT student.name, department.name
FROM student LEFT JOIN department ON student.deptartment =
department.name
Right Join :
It is the join which gets all the records that match the given condition, and
also fetch all the records from
the right table.
Example :
SELECT student.name, department.name
FROM student RIGHT JOIN department on student.department =
department.name
35. ADD or DROP a column
A new column can be added on a database table, if required later on.
Example :
ALTER TABLE employee ADD COLUMN salary VARCHAR(25);
Similarly, any column can be deleted from a database table.
Example :
ALTER TABLE employee DROP COLUMN salary;
Conclusion :
Running MySQL queries are the most commonly-performed tasks used to get
data within the process of database management. There are many database
management tools like phpMyAdmin, that helps to perform queries and
handle transactions comfortably with visual data results. You have scrolled
the most common MySQL queries, which are used in daily coding practices.
Developer can customize or enhance the queries with respect to a particular
requirement.
Introduction to NoSQL
NoSQL, or “Not Only SQL,” is a database management system (DBMS)
designed to handle large volumes of unstructured and semi-structured data.
Unlike traditional relational databases that use tables and pre-defined
schemas, NoSQL databases provide flexible data models and
support horizontal scalability, making them ideal for modern applications that
require real-time data processing.
Why Use NoSQL?
Unlike relational databases, which uses Structured Query Language, NoSQL
databases don’t have a universal query language. Instead, each type of
NoSQL database typically has its unique query language. Traditional relational
databases follow ACID (Atomicity, Consistency, Isolation,
Durability) principles, ensuring strong consistency and structured relationships
between data.
However, as applications evolved to handle big data, real-time analytics, and
distributed environments, NoSQL emerged as a solution with:
Scalability – Can scale horizontally by adding more nodes instead of
upgrading a single machine.
Flexibility – Supports unstructured or semi-structured data without a
rigid schema.
High Performance – Optimized for fast read/write operations with
large datasets.
Distributed Architecture – Designed for high availability and
partition tolerance in distributed systems.
Types of NoSQL Databases
NoSQL databases are generally classified into four main categories based on
how they store and retrieve data
1. Document databases
Store data in JSON, BSON, or XML format.
Data is stored as documents that can contain varying attributes.
Examples: MongoDB, CouchDB, Cloudant
Ideal for content management systems, user profiles, and catalogs
where flexible schemas are needed.
2. Key-value stores
Data is stored as key-value pairs, making retrieval extremely fast.
Optimized for caching and session storage.
Examples: Redis, Memcached, Amazon DynamoDB
Perfect for applications requiring session management, real-time
data caching, and leaderboards.
3. Column-family stores
Data is stored in columns rather than rows, enabling high-speed
analytics and distributed computing.
Efficient for handling large-scale data with high write/read
demands.
Examples: Apache Cassandra, HBase, Google Bigtable
Great for time-series data, IoT applications, and big data analytics.
4. Graph databases
Data is stored as nodes and edges, enabling complex relationship
management.
Best suited for social networks, fraud detection, and
recommendation engines.
Examples: Neo4j, Amazon Neptune, ArangoDB
Useful for applications requiring relationship-based queries such as
fraud detection and social network analysis.
Key Features of NoSQL Databases
1. Dynamic schema: NoSQL databases do not have a fixed schema and
can accommodate changing data structures without the need for
migrations or schema alterations.
2. Horizontal scalability: NoSQL databases are designed to scale out
by adding more nodes to a database cluster, making them well-
suited for handling large amounts of data and high levels of traffic.
3. Document-based: Some NoSQL databases, such as MongoDB, use a
document-based data model, where data is stored in a schema-less
semi-structured format, such as JSON or BSON.
4. Key-value-based: Other NoSQL databases, such as Redis, use a key-
value data model, where data is stored as a collection of key-value
pairs.
5. Column-based: Some NoSQL databases, such as Cassandra, use a
column-based data model, where data is organized into columns
instead of rows.
6. Distributed and high availability: NoSQL databases are often
designed to be highly available and to automatically handle node
failures and data replication across multiple nodes in a database
cluster.
7. Flexibility: NoSQL databases allow developers to store and retrieve
data in a flexible and dynamic manner, with support for multiple data
types and changing data structures.
8. Performance: NoSQL databases are optimized for high performance
and can handle a high volume of reads and writes, making them
suitable for big data and real-time applications.
Advantages of NoSQL
There are many advantages of working with NoSQL databases such as
MongoDB and Cassandra. The main advantages are high scalability and high
availability.
1. High scalability: NoSQL databases use sharding for horizontal
scaling. Partitioning of data and placing it on multiple machines in
such a way that the order of the data is preserved is sharding.
Vertical scaling means adding more resources to the existing
machine whereas horizontal scaling means adding more machines to
handle the data. Vertical scaling is not that easy to implement but
horizontal scaling is easy to implement. Examples of horizontal
scaling databases are MongoDB, Cassandra, etc. NoSQL can handle
a huge amount of data because of scalability, as the data grows
NoSQL scalesThe auto itself to handle that data in an efficient
manner.
2. Flexibility: NoSQL databases are designed to handle unstructured or
semi-structured data, which means that they can accommodate
dynamic changes to the data model. This makes NoSQL databases a
good fit for applications that need to handle changing data
requirements.
3. High availability: The auto, replication feature in NoSQL databases
makes it highly available because in case of any failure data
replicates itself to the previous consistent state.
4. Scalability: NoSQL databases are highly scalable, which means that
they can handle large amounts of data and traffic with ease. This
makes them a good fit for applications that need to handle large
amounts of data or traffic
5. Performance: NoSQL databases are designed to handle large
amounts of data and traffic, which means that they can offer
improved performance compared to traditional relational databases.
6. Cost-effectiveness: NoSQL databases are often more cost-effective
than traditional relational databases, as they are typically less
complex and do not require expensive hardware or software.
7. Agility: Ideal for agile development.
Disadvantages of NoSQL
1. Lack of standardization: There are many different types of NoSQL
databases, each with its own unique strengths and weaknesses. This
lack of standardization can make it difficult to choose the right
database for a specific application
2. Lack of ACID compliance: NoSQL databases are not fully ACID-
compliant, which means that they do not guarantee the consistency,
integrity, and durability of data. This can be a drawback for
applications that require strong data consistency guarantees.
3. Narrow focus: NoSQL databases have a very narrow focus as it is
mainly designed for storage but it provides very little functionality.
Relational databases are a better choice in the field of Transaction
Management than NoSQL.
4. Open-source: NoSQL is an databaseopen-source database. There is
no reliable standard for NoSQL yet. In other words, two database
systems are likely to be unequal.
5. Lack of support for complex queries: NoSQL databases are not
designed to handle complex queries, which means that they are not a
good fit for applications that require complex data analysis or
reporting.
6. Lack of maturity: NoSQL databases are relatively new and lack the
maturity of traditional relational databases. This can make them less
reliable and less secure than traditional databases.
7. Management challenge: The purpose of big data tools is to make the
management of a large amount of data as simple as possible. But it is
not so easy. Data management in NoSQL is much more complex than
in a relational database. NoSQL, in particular, has a reputation for
being challenging to install and even more hectic to manage on a
daily basis.
8. GUI is not available: GUI mode tools to access the database are not
flexibly available in the market.
9. Backup: Backup is a great weak point for some NoSQL databases like
MongoDB. MongoDB has no approach for the backup of data in a
consistent manner.
10. Large document size: Some database systems like MongoDB and
CouchDB store data in JSON format. This means that documents are
quite large (BigData, network bandwidth, speed), and having
descriptive key names actually hurts since they increase the
document size.
SQL vs. NoSQL: A Comparison
Feature SQL (Relational DB) NoSQL (Non-Relational DB)
ACID
Strong Limited or Eventual Consistency
Support
Transactional
Best For Big data, real-time analytics
applications
MySQL, PostgreSQL,
Examples MongoDB, Cassandra, Redis
Oracle
Column-Family
Cassandra Big data, high availability systems
Store
NoSQL
Database Type Use Cases
Conclusion
NoSQL databases provide a flexible, scalable, and high-
performance alternative to traditional relational databases, making them ideal
for modern applications like real-time analytics, big data processing, and web
applications. However, they come with trade-offs, such as a lack of ACID
compliance and more complex management. When choosing a database for
your application, it’s essential to evaluate your data needs, consistency
requirements, and scalability goals to determine whether NoSQL or a relational
database is the best fit.
Disadvantages of NoSQL :
The system built with NoSQL is fundamentally non-transactional.
The volume of data created is huge and does not offer any traditional
database capability.
It does not follow consistency when multiple transactions are
performed simultaneously.
2. NewSQL :
The term NewSQL categorizes databases that are the combination of
relational model with the advancement in scalability, flexibility with types of
data. These databases focus on the features which are not present in NoSQL,
which offers a strong consistency guarantee. This covers two layers of data
one relational one and a key-value store.
Advantages of NewSQL :
It introduces new implementation to traditional relational databases.
It brings together the advantages of SQL and NoSQL.
It is easy to migrate between the type and needs of the user.
Disadvantages of NewSQL :
They offer partial access to rich traditional systems.
It may cause a problem in-memory architecture for exceeding
volumes of data.
The core foundation of such databases is relational systems which
make it tricky to understand.
Difference between NoSQL and NewSQL :
S.No NoSQL NewSQL
It supports cloud, on-disk, and cache It fully supports cloud, on-disk, and
4.
storage. cache storage.
Use Cases: Big Data, Social Network Use Cases: E-Commerce, Telecom
8.
Applications, and IOT. industry, and Gaming.
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
VALUES ('5','HARSH','WEST BENGAL', 'XXXXXXXXXX','19');
Output
ROLL_NO NAME ADDRESS PHONE Age
Note: Columns not included in the INSERT statement are filled with default
values (typically NULL).
Inserting Multiple Rows at Once
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
INSERT INTO table_name (column1, column2, …)
VALUES
(value1, value2, …),
(value1, value2, …),
(value1, value2, …);
Example:
If we want to add multiple students to the Students table in one go, the query
would look like this:
INSERT INTO Student (ROLL_NO, NAME, AGE, ADDRESS, PHONE)
VALUES
(6, 'Amit Kumar', 15, 'Delhi', 'XXXXXXXXXX'),
(7, 'Gauri Rao', 18, 'Bangalore', 'XXXXXXXXXX'),
(8, 'Manav Bhatt', 17, 'New Delhi', 'XXXXXXXXXX'),
(9, 'Riya Kapoor', 10, 'Udaipur', 'XXXXXXXXXX');
Output:
ROLL_NO NAME ADDRESS PHONE AGE
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.
Inserting Data from One Table into Another Table
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.
Syntax 1: Insert All Columns from Another Table
INSERT INTO target_table
SELECT * FROM source_table;
Example: If you want to copy all data from the OldStudents table into
the Students table, use this query:
INSERT INTO Students
SELECT * FROM OldStudents;
Syntax 2: Insert Specific Columns from Another Table
INSERT INTO target_table (col1, col2, …)
SELECT col1, col2, …
FROM source_table;
Example: Let’s say we want to copy only the Name and Age columns
from OldStudents into Students:
INSERT INTO Students (Name, Age)
SELECT Name, Age
FROM OldStudents;
Syntax 3: Insert Specific Rows Based on Condition
You can also insert specific rows based on a condition by using
the WHERE clause with the SELECT statement.
INSERT INTO target_table
SELECT * FROM source_table
WHERE condition;
Example: If we want to copy only students older than 20 years
from OldStudents to Students, we would write:
INSERT INTO Students
SELECT * FROM OldStudents
WHERE Age > 20;
Important Points About SQL INSERT INTO Statement
Multiple Inserts: You can insert multiple rows at once by separating
each set of values with commas. This reduces the number of queries
you need to run.
NULL Values: If you don’t insert data into a column, it will typically
be set to NULL unless the column has a default value.
Order of Columns: When using the simple INSERT INTO syntax
without specifying column names, the values must be in the exact
same order as the columns are defined in the table.
Default Values: Columns not mentioned in the INSERT
INTO statement will be filled with their default values (often NULL).
Efficiency: Inserting multiple rows at once is much more efficient
than doing it one by one.
Conclusion
Overall, INSERT INTO statement is an essential feature in SQL for adding new
data to tables. Whether you’re adding a single row or multiple rows, specifying
column names or copying data from another table, INSERT INTO provides the
necessary flexibility. Understanding its syntax and usage is crucial for efficient
database management and data manipulation.
GFG_Employees
output
Parameters
UPDATE: The SQL command used to modify the data in a table.
SET: This clause defines which columns will be updated and what
their new values will be.
WHERE: The condition that determines which rows will be updated.
Without it, all rows will be affected.
table_name: name of the table in which you want to make updates
column1, column2, …: The columns you want to update.
value1, value2, …: The new values to assign to these columns.
condition: Specifies which rows to update. This condition is crucial, as
omitting it will update all rows in the table.
Note: In the above query the SET statement is used to set new values to the
particular column and the WHERE clause is used to select the rows for which
the columns are needed to be updated. If we have not used the WHERE clause
then the columns in all the rows will be updated. So the WHERE clause is used
to choose the particular rows.
Examples of SQL UPDATE Statement
Let’s start by creating a table and inserting some sample data, which we will
use for the UPDATE statement examples. The Customer table stores information
about individual customers, including their unique CustomerID, personal details
like CustomerName and LastName, as well as their contact details such
as Phone and Country.
Query:
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age,
Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain',
'Spain','22','xxxxxxxxxx');
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.
Optimizing Your SQL UPDATE Queries
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.
Important Points About SQL UPDATE Statement
1. Always use the WHERE clause: The most important point when using
the UPDATE statement is to always include a WHERE clause unless you genuinely
intend to update all rows.
2. Check your data before updating: Run a SELECT query to view the data you
want to update before executing the UPDATE statement. This helps avoid
accidental data modifications.
SELECT * FROM Customer WHERE Age = 22;
3. Use transactions for critical updates: When performing updates in a
production environment, consider using transactions. Transactions allow you to
commit or roll back changes as needed.
BEGIN TRANSACTION;
UPDATE Customer SET CustomerName = 'John' WHERE CustomerID = 3;
COMMIT; -- Use ROLLBACK to undo if necessary
4. Test on a small dataset first: If you’re uncertain about the impact of
your UPDATE, test it on a small subset of data to ensure the changes are as
expected.
Conclusion
The SQL UPDATE statement is use for modifying existing data in
your database. Whether you’re performing simple updates or handling complex
logic with subqueries, joins, and case expressions, SQL provides all the
flexibility we need. By understanding the basic syntax, advanced techniques,
and common pitfalls, we can ensure that our UPDATE queries are both
efficient and error-free. By following best practices and paying attention to
details, you can safely and effectively perform updates in your database.
Note: If you’re working with a large amount of data, consider using bulk
inserts or importing data from external files to optimize performance.
Create Table From Another Table
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:
CREATE TABLE new_table_name AS
SELECT column1, column2,…
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:
CREATE TABLE SubTable AS
SELECT CustomerID, CustomerName
FROM customer;
Output:
Note: We can use * instead of column name to copy whole table to another
table.
Important Points About SQL CREATE TABLE Statement
To ensure the smooth creation and management of your tables, keep these
points in mind:
1. Adding Constraints: The CREATE TABLE statement can also define
constraints like NOT NULL, UNIQUE, and DEFAULT. Example: Age INT NOT
NULL
2. Handling Table Existence: 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.
CREATE TABLE IF NOT EXISTS Customer (...);
3. Data Types and Sizes: Always define appropriate data types for each
column (e.g., VARCHAR(50) for names and INT for IDs) to optimize performance
and storage.
4. Viewing Table Structure: After creating a table, use the following command
to view the structure of your table:
DESC table_name;
5. Modifying Table Structure: 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.
Conclusion
The SQL CREATE TABLE statement is essential for setting up tables and
defining data structures within a database. Knowing how to create a table
in SQL with the CREATE TABLE SQL syntax helps you establish reliable tables
with appropriate constraints. Whether you’re creating an Employee table in
SQL with primary keys or duplicating tables with CREATE TABLE AS SELECT,
mastering this command is vital for managing data effectively.
In this guide, we covered SQL CREATE TABLE syntax with various examples,
from basic to advanced usage. By applying these practices, you can confidently
build tables in SQL for effective data organization and retrieval.
DROP and TRUNCATE in SQL
The DROP and TRUNCATE commands in SQL are used to remove data from a
table, but they work differently. Understanding the difference between these
two commands is important for proper database management, especially
when dealing with large amounts of data.
This article provides an in-depth explanation of the DROP and
TRUNCATE commands in SQL, their main differences, usage, and detailed
examples. By the end of this article, We will be ready to use these commands
effectively and understand their impact on your database.
What is the SQL DROP Command?
In SQL, the DROP command is used to permanently remove an object from a
database, such as a table, database, index, or view. When we DROP a table,
both the data and the structure of the object are permanently removed from the
database leaving no trace of the object.
Syntax:
DROP object object_name ;
Key Terms
object: The type of object you want to drop (e.g., TABLE,
DATABASE).
object_name: The name of the object to be deleted.
DROP Command Examples
Let’s look at some examples of the DROP statement in SQL.
1. DROP Table
To delete an entire table, including its data and structure:
Syntax:
DROP TABLE table_name;
2. DROP database
To delete an entire database and all of its associated tables:
Syntax:
DROP DATABASE database_name;
Effect on Table Completely removes both the Removes all rows but
Structure data and the table structure preserves the table structure
Deletes both data and table Deletes only the data, not the
Data Deletion
definition table structure
Non-recoverable; once
Can be rolled back if used in
dropped, the table cannot be
Recovery a transaction (if supported by
restored (unless you have
the DBMS)
backups)
Feature DROP TRUNCATE
Student Table
Conclusion
In SQL, both DROP and TRUNCATE commands are used for removing data,
but their impacts are different. DROP is used when you want to completely
remove a table or database, while TRUNCATE is more efficient for removing all
rows from a table without affecting its structure. Understanding when and how
to use these commands will help you maintain better control over your
database and optimize your database management processes.
1 Ram
2 Abhi
3 Rahul
4 Tanu
1 Ram
2 Abhi
3 Rahul
4 Tanu
Explanation: This adds the AGE column with a numeric data type and
the COURSE column with a VARCHAR(40) type. The columns are added to the
table, but the values are empty initially.
2. Modify Column COURSE to Reduce its Size
If you want to reduce the size of the COURSE column
from VARCHAR(40) to VARCHAR(20), you can modify the column’s data type using
the MODIFY clause.
Query:
ALTER TABLE Student
MODIFY COURSE varchar(20);
Explanation: After running the above query, the COURSE column’s data type
will now allow a maximum of 20 characters instead of 40. This modifies the
column’s size limit.
3. Drop the COURSE Column from the Student Table
To remove the COURSE column completely from the Student table, you can
use the DROP COLUMN clause.
Query:
ALTER TABLE Student
DROP COLUMN COURSE;
Output:
ROLL_NO NAME
1 Ram
2 Abhi
3 Rahul
4 Tanu
Example
SELECT * FROM customers;
Explanation:
In this example, the comment -- query to fetch customer records explains
the purpose of the query, which is to retrieve all records from
the customers table.
2. SQL Multi-Line Comments
Multi-line comments are used when we need to comment out more than one
line of SQL code. These comments are enclosed between /* and */ and can
span multiple lines. They are typically used for longer explanations or to
disable larger sections of code temporarily.
Syntax:
/* multi line comment
another comment */
Example
SELECT *
FROM orders
WHERE YEAR(order_date) = 2022;
Explanation:
In this example, the multi-line comment explains that the query retrieves all
orders placed in the year 2022 from the orders table.
3. SQL In-Line Comments
In-line comments allow us to add comments within a query itself. They are
typically used to provide additional information or explanations for specific
parts of the query, without interrupting the flow of the SQL statement. In-line
comments start with /* and end with */.
Syntax:
SELECT * FROM /* Customers; */
Example
SELECT customer_name,
order_date
FROM orders;
Explanation:
In this example, in-line comments are used to describe the purpose of
the customer_name and order_date columns. This approach keeps the code
readable and helps future developers understand the logic quickly.
Important Points About SQL Comments
While SQL comments are crucial for code documentation, there are several best
practices to follow when using them:
1. Be Concise but Clear: Keep comments brief but descriptive. Avoid
unnecessary comments that do not add value to the code.
2. Explain Complex Queries: Use comments to explain why certain
decisions were made, especially for complex queries or business
logic.
3. Avoid Commenting Obvious Code: Do not comment on trivial or
self-explanatory code. For example, there is no need to comment
simple SELECT statements with straightforward column names.
4. Use Comments to Temporarily Disable Code: Comments are often
used to “comment out” parts of SQL code that need to be tested or
temporarily disabled.
5. Consistent Formatting: Use a consistent style for comments
throughout your SQL codebase. This helps make the code easier to
read and understand, especially when working with a team.
Conclusion
SQL comments are an essential tool for any developer working
with databases. They allow us to annotate our code, making it easier
to understand, maintain, and debug. Whether we’re using single-line
comments for quick explanations, multi-line comments for more detailed
descriptions, or in-line comments for specific parts of a query, proper
commenting practices will improve the clarity of our SQL code.
ALTER (RENAME) in SQL
In SQL, making structural changes to a database is often necessary. Whether
it’s renaming a table or a column, adding new columns, or modifying data types,
the SQL ALTER TABLE command plays a critical role. This command provides
flexibility to manage and adjust database schemas without affecting the
existing data. In this article, we will explain how to rename tables and
columns in SQL using the ALTER TABLE command, along with practical
examples.
What is ALTER Command in SQL?
The ALTER TABLE command in SQL allows us to modify the structure of an
existing table. Whether we need to rename a table, rename a column, add a
new column, or change the data type of a column, this command makes it easy
to apply changes without affecting the data already stored.
The ALTER command is fundamental for database management and is often
used by developers to keep schemas aligned with evolving application needs.
Here are some common tasks you can achieve using the ALTER command:
Renaming a table.
Changing a column name.
Adding or deleting columns.
Modifying the data type of a column.
Syntax for ALTER Command
1. Renaming a Table
ALTER TABLE table_name
RENAME TO new_table_name;
2. Renaming a Column
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;
3. Adding a New Column
ALTER TABLE table_name
ADD column_name datatype;
4. Modifying a Column Data Type
ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype;
Student Table
Output
Student_Details table
Explanation:
The phone column now has a BIGINT data type, suitable for storing
large numeric values.
Existing data remains unchanged but is stored as integers instead
of strings.
Additional ALTER Command Use Cases
1. Removing a Column: In some cases, we might need to remove a column.
To do that, you can use the DROP COLUMN syntax:
ALTER TABLE Student_Details
DROP COLUMN marks;
This command deletes the marks column entirely from the table
2. Changing a Column’s Default Value: We can also modify a column’s
default value using the SET DEFAULT clause:
ALTER TABLE Student_Details
ALTER COLUMN age SET DEFAULT 18;
3. Renaming a Table or Column in Different Databases: Note that SQL syntax
can vary across different database systems. Here’s how we would rename a
table or column in MySQL, MariaDB, and Oracle:
MySQL / MariaDB: The syntax for renaming a column is similar, but
you must also use the CHANGE COLUMN command to rename a
column:
ALTER TABLE Student
CHANGE COLUMN old_column_name new_column_name datatype;
Oracle: Oracle supports the RENAME COLUMN syntax but requires
different syntax for renaming a table:
ALTER TABLE Student RENAME COLUMN old_column_name TO
new_column_name;
Conclusion
The SQL ALTER TABLE command is an effective way to modify the
structure of an already-existing tables in a database. When necessary, we can
use ALTER TABLE to rename the entire table, rename a specific column in SQL,
or change a column name to something more descriptive. This command is
important for database administration since it also enables the addition of new
columns and the modification of data types.
Query:
SELECT Department, sum(Salary) as Salary
FROM employee
GROUP BY department
HAVING SUM(Salary) >= 50000;
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:
SELECT * FROM Customer ORDER BY Age DESC;
Output:
Fetch unique
SELECT DISTINCT column FROM table;
values
Goal Technique
Conclusion
The SQL SELECT statement is an essential tool for retrieving and analyzing
data from relational databases. Whether we’re fetching specific columns or
using advanced clauses like WHERE, GROUP BY, and ORDER BY, the SELECT
query provides flexibility for efficient data retrieval. By understanding how to
use the SELECT statement and combining it with various clauses, we can
efficiently filter, aggregate, and sort data to meet our needs.
If explicitly “NOT NULL” is specified, that column should have values. If not
specified, it is by default “NULL”.
“Authors” named table is created under “GEEKSFORGEEKS” database
Here we are taking into consideration the mentioned columns and hence only
their required values are inserted by the above query.
Example 2 :
INSERT INTO <table_name> VALUES (value1, value2, value3, ...);
Here we are not specifying any columns means, all the values to all the
columns need to be inserted.
Violation of PRIMARY KEY constraint 'PK__Authors__3214EC277EBB8ED1'.
Cannot insert duplicate key in object 'dbo.Authors'. The duplicate key
value is (1).
The above errors occurred in the above screenshot shows that “ID” column is
unique and it should not have duplicate values
Now, let us correct that and query the table by using:
SELECT * FROM <tablename>
It is observed that Row 1 is having ‘Null’ values in the place of ‘Skillsets’ and
‘NumberOfPosts’ column. The reason is as we have not specified values for
those columns, it has taken default Null values.
SQL Locks :
SQL Server is a relational database, data consistency is an important
mechanism, and it can be done by means of SQL Locks. A lock is established
in SQL Server when a transaction starts, and it is released when it is ended..
There are different types of locks are there.
Shared (S) Locks: When the object needs to be read, this type of lock
will occur, but this is not harmful.
Exclusive (X) Locks: It prevents other transactions like
inserting/updating/deleting etc., Hence no modifications can be done
on a locked object.
Update (U) Locks: More or less similar to Exclusive lock but here the
operation can be viewed as “read phase” and “write phase”.
Especially during the read phase, other transactions are prevented.
Intent Locks: When SQL Server has the shared (S) lock or exclusive
(X) lock on a row, then the intent lock is on the table.
Regular intent locks: Intent exclusive (IX) , Intent shared (IS), and
Intent update (IU).
Conversion locks: Shared with intent exclusive (SIX), Shared with
intent update (SIU), and Update with intent exclusive (UIX).
Lock hierarchy starts from Database, then table, then row.
Let us insert some more records(nearly around 100 records) into the table
and then using a transaction, let us update few columns as well as parallel
apply the select query also
--Let us create an open transaction and analyze the locked resources.
BEGIN TRAN
--Let us update the Skillsets when ID < 20
UPDATE Authors SET Skillsets='Java,Android,R Programming' where ID < 20
--Let us update the Skillsets when ID >= 25
UPDATE Authors SET Skillsets='Android,IOS,R Programming' where ID >= 25
--Other DML statements like Update/Delete. This statement must be
taking a long time
--(if there are huge updates are happening) as previous statement
itself
--is either not committed or rolled back yet
SELECT * FROM Authors;
select @@SPID
Actually when the earlier command transaction are not yet complete(if there
are huge records at least 100 records) and update is happening on each and
every row and before completion of it, if we are proceeding for another set of
commands like “select”
Then there are possibilities of the status will be “Awaiting” (Queries which are
executing) and “Suspended” (Queries which are halt)
How to overcome the so far running process?
KILL <spid> -> Kill the session
By doing this process, we are enforcing the operation either to get committed
or rolled back(depends upon the requirements, it has to be carried out)
But unless we know that the entire process is required or not, we cannot
either commit or rollback the transaction.
Alternative way :
By using NOLOCK with SELECT QUERY, we can overcome
SELECT * FROM Authors WITH (NOLOCK);
For SELECT statement status using the sp_who2 command. The query runs
without waiting for the UPDATE transaction to be completed successfully and
release the locking on the table,
SELECT * FROM Authors WITH (READUNCOMMITTED);
--This way also we can do
Conclusion :
SQL Locks are much important for any RDBMS. SQL Server handles them in
the mentioned ways.
Example:
grant insert,
select on accounts to Ram
By the above command user ram has granted permissions on accounts
database object like he can query or insert into accounts.
revoke insert,
select on accounts from Ram
To know the exact syntax and how are they used click here.
Differences between Grant and Revoke commands:
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?
What is an SQL Transaction?
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.
Key Properties of SQL Transactions: ACID
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.
Consistency: Transactions maintain integrity restrictions by moving
the database from one valid state to another.
Isolation: Concurrent transactions are isolated from one another,
assuring the accuracy of the data.
Durability: Once a transaction is committed, its modifications remain
in effect even in the event of a system failure.
SQL Transaction Control Commands
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.
1. BEGIN TRANSACTION Command
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:
BEGIN TRANSACTION transaction_name ;
Example of SQL Transaction with a Bank Transfer Scenario
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;
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.
DELETE FROM Student WHERE AGE = 20;
COMMIT;
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.
DELETE FROM Student WHERE AGE = 20;
ROLLBACK;
Output
output
4. SAVEPOINT Command
A SAVEPOINT is used to create a checkpoint within a transaction. We can roll
back to a specific SAVEPOINT instead of rolling back the entire transaction. This
allows us to undo part of the transaction rather than the entire transaction.
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:
ROLLBACK TO SAVEPOINT SAVEPOINT_NAME;
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
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:
= Equal to
Conclusion
The WHERE clause is use for filtering and refining SQL queries. Whether you’re
working with basic conditions, using logical operators, or performing advanced
queries with subqueries and EXISTS, mastering the WHERE clause is essential
for every SQL user. Understanding how to efficiently filter data, avoid common
pitfalls, and optimize your queries will ensure you’re able to write clean, fast,
and accurate SQL queries.
Now if we need to display the departments where the sum of salaries is 50,000
or more. In this condition, we will use the HAVING Clause.
SELECT Department, sum(Salary) as Salary
FROM Employee
GROUP BY department
HAVING SUM(Salary) >= 50000;
Output:
IT 2
This query counts the number of employees in each department and uses the
HAVING clause to filter for departments with more than two employees.
Example 4: Using HAVING with AVG()
In this example, let’s find out the average salary for each department and use
the HAVING clause to display only those departments where the average
salary is greater than $50,000.
Query:
SELECT Department, AVG(Salary) AS Average_Salary
FROM Employee
GROUP BY Department
HAVING AVG(Salary) > 50000;
Output:
Department Average_Salary
Finance 75000
Marketing 55000
Department Average_Salary
Sales 65000
Having vs WHERE
Having Where
In the HAVING clause it will check In the WHERE condition it will check or
the condition in group of a row. execute at each row individual.
HAVING clause can only be used The WHERE Clause cannot be used with
with aggregate function. aggregate function like Having
Conclusion
The HAVING clause is an essential tool in SQL for filtering results based
on aggregated data. Unlike the WHERE clause, which applies conditions to
individual rows, HAVING works on groups of data that have been aggregated
using functions like SUM(), AVG(), and COUNT(). Understanding how and
when to use the HAVING clause allows you to perform more complex data
analysis and generate meaningful insights from your datasets.
SQL | GROUP BY
The SQL GROUP BY clause is a powerful tool used to organize data into groups
based on shared values in one or more columns. It’s most often used with
aggregate functions like SUM, COUNT, AVG, MIN, and MAX to perform summary
operations on each group helping us extract meaningful insights from large
datasets.
Whether we’re analyzing sales by region, users by age group, or orders by
product category, the GROUP BY clause helps transform raw data into
structured reports.
What is GROUP BY Clause in SQL?
The GROUP BY statement in SQL is used to arrange identical data into groups
based on specified columns. If a particular column has the same values in
multiple rows, the GROUP BY clause will group these rows together. It’s
commonly used with aggregate functions to calculate totals or averages per
group. Key Points About GROUP BY:
GROUP BY clause is used with the SELECT statement.
In the query, the GROUP BY clause is placed after
the WHERE clause.
In the query, the GROUP BY clause is placed before the ORDER BY
clause if used.
In the query, the GROUP BY clause is placed before the Having
clause.
Place condition in the having clause.
Syntax:
SELECT column1, function_name(column2)
FROM table_name
GROUP BY column1, column2
Key Terms
1. function_name: Name of the function used for example, SUM() ,
AVG().
2. table_name: Name of the table.
3. condition: Condition used.
Examples of GROUP BY in SQL
Let’s assume that we have two tables Employee and Student Sample Table is
as follows after adding two tables we will do some specific operations to learn
about GROUP BY. Insert some random data into a table and then we will
perform some operations in GROUP BY.
Employee Table
CREATE TABLE emp (
emp_no INT PRIMARY KEY,
name VARCHAR(50),
sal DECIMAL(10,2),
age INT
);
Student Table
CREATE TABLE student (
name VARCHAR(50),
year INT,
subject VARCHAR(50)
);
Output
Explanations:
As you can see in the above output, the rows with duplicate NAMEs are
grouped under the same NAME and their corresponding SALARY is the sum of
the SALARY of duplicate rows. The SUM() function of SQL is used here to
calculate the sum. The NAMES that are added are Aarav, Divya and Gaurav.
Example 2 : Group By Multiple Columns
Group by multiple columns is say, for example, GROUP BY column1, column2.
This means placing all the rows with the same values of columns column
1 and column 2 in one group. This SQL query groups student records by
both SUBJECT and YEAR and then counts the number of records (i.e., students)
in each of those groups.
Query:
SELECT SUBJECT, YEAR, Count(*)
FROM Student
GROUP BY SUBJECT, YEAR;
Output:
Output
Explantions:
As we can see in the above output the students with both the same SUBJECT
and YEAR are placed in the same group. And those whose only SUBJECT is the
same but not YEAR belong to different groups. So here we have grouped the
table according to two columns or more than one column. The Grouped subject
and years are (English,2) , (Mathematics,1) and (Science,3). The above
mentioned all groups and years are repeated twice.
HAVING Clause in GROUP BY Clause
We know that the WHERE clause is used to place conditions on columns but
what if we want to place conditions on groups? This is where the HAVING
clause comes into use. We can use the HAVING clause to place conditions to
decide which group will be part of the final result set. Also, we can not use
aggregate functions like SUM(), COUNT(), etc. with the WHERE clause. So we
have to use the HAVING clause if we want to use any of these functions in the
conditions.
Syntax:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2
HAVING condition
ORDER BY column1, column2;
Key Terms
function_name: Name of the function used for example, SUM() ,
AVG().
table_name: Name of the table.
condition: Condition used.
Example:
SELECT NAME, SUM(sal) FROM Emp
GROUP BY name
HAVING SUM(sal)>50000;
Output:
Output
Explanation:
In the result, only employees whose total salary (SUM(sal)) exceeds 50,000 are
displayed. For example, if Anjali has a total salary less than 50,000, she will be
excluded from the output.
Conclusion
The GROUP BY function in SQL organizes identical data into groups, enabling
aggregate analysis on each group. It is commonly used with aggregate
functions like SUM(), COUNT(), AVG(), etc., to summarize data efficiently.
The HAVING clause further refines the results by applying conditions to these
grouped records. GROUP BY can operate on single or multiple columns,
making it a versatile tool for data retrieval and reporting.