Learn
Learn
SQL
What is a Database?
A database is a digital system designed for the storage and arrangement of data.
Think of it as an online filing system that allows you to store and quickly access a
vast amount of information. Databases facilitate the efficient management of data,
enabling the simple addition, modification, removal, and access of information. They
serve numerous uses such as websites, applications, and enterprises to manage
extensive data in an organized and secure manner.
What is MySQL?
MySQL is an open-source relational database management system (RDBMS) that
uses Structured Query Language (SQL) to manage data. Developed by MySQL
AB and now owned by Oracle Corporation, it’s widely used due to
its reliability, speed, and ease of use. MySQL is a key component in many web
applications, forming the backbone of popular websites and services.
It allows users to create, modify, and maintain databases, supporting operations like
data insertion, querying, updating, and deletion. Ideal for both small and large-scale
applications, MySQL powers various types of systems, from personal projects to
complex enterprise environments.
SQL is a Structured query language used to access and manipulate data in
databases. SQL is a query language that communicates with databases.
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.
Important Terminologies
These are some important terminologies that are used in terms of relation.
Table STUDENT
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
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
Table control
LOCK LOCK TABLE table_name IN lock_mode;
concurrency
SQL CONSTRAINTS:
SQL constraints are essential elements in relational database design that ensure
the integrity, accuracy, and reliability of the data stored in a database. By enforcing
specific rules on table columns, SQL constraints help maintain data consistency,
preventing invalid data entries and optimizing query performance.
In this article, we will explain the most common SQL constraints in detail, providing
clear examples and explaining how to implement them effectively.
What Are SQL Constraints?
SQL constraints are rules applied to columns or tables in a relational database to
limit the type of data that can be inserted, updated, or deleted. These rules ensure
the data is valid, consistent, and adheres to the business logic or database
requirements. Constraints can be enforced during table creation or later using
the ALTER TABLE statement. They play a vital role in maintaining the quality and
integrity of your database.
Types of SQL Constraints
SQL provides several types of constraints to manage different aspects of data
integrity. These constraints are essential for ensuring that data meets the
requirements of accuracy, consistency, and validity. Let’s go through each of them
with detailed explanations and examples.
1. NOT NULL Constraint
The NOT NULL constraint ensures that a column cannot contain NULL values. This is
particularly important for columns where a value is essential for identifying records
or performing calculations. If a column is defined as NOT NULL, every row must
include a value for that column.
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
ADDRESS varchar(20)
);
Explanation: In the above example, both the ID and NAME columns are defined with
the NOT NULL constraint, meaning every student must have an ID and NAME value.
2. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are distinct across all
rows in a table. Unlike the PRIMARY KEY, which requires uniqueness and does not
allow NULLs, the UNIQUE constraint allows NULL values but still enforces
uniqueness for non-NULL entries.
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20)
);
Explanation: Here, the ID column must have unique values, ensuring that no two
students can share the same ID. We can have more than one UNIQUE constraint in a
table.
3. PRIMARY KEY Constraint
A PRIMARY KEY constraint is a combination of the NOT
NULL and UNIQUE constraints. It uniquely identifies each row in a table. A table can
only have one PRIMARY KEY, and it cannot accept NULL values. This is typically
used for the column that will serve as the identifier of records.
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20),
PRIMARY KEY(ID)
);
Explanation: In this case, the ID column is set as the primary key, ensuring that each
student’s ID is unique and cannot be NULL.
4. FOREIGN KEY Constraint
A FOREIGN KEY constraint links a column in one table to the primary key in another
table. This relationship helps maintain referential integrity by ensuring that the
value in the foreign key column matches a valid record in the referenced table.
Orders Table:
1 2253 3
2 3325 3
3 4521 2
4 8532 1
Customers Table:
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON
As we can see clearly that the field C_ID in Orders table is the primary key in
Customers table, i.e. it uniquely identifies each row in the Customers table.
Therefore, it is a Foreign Key in Orders table.
Example:
CREATE TABLE Orders
(
O_ID int NOT NULL,
ORDER_NO int NOT NULL,
C_ID int,
PRIMARY KEY (O_ID),
FOREIGN KEY (C_ID) REFERENCES Customers(C_ID)
)
Explanation: In this example, the C_ID column in the Orders table is a foreign key that
references the C_ID column in the Customers table. This ensures that only valid
customer IDs can be inserted into the Orders table.
5. CHECK Constraint
The CHECK constraint allows us to specify a condition that data must satisfy before
it is inserted into the table. This can be used to enforce rules, such as ensuring that a
column’s value meets certain criteria (e.g., age must be greater than 18)
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18)
);
Explanation: In the above table, the CHECK constraint ensures that only students
aged 18 or older can be inserted into the table.
6. DEFAULT Constraint
The DEFAULT constraint provides a default value for a column when no value is
specified during insertion. This is useful for ensuring that certain columns always
have a meaningful value, even if the user does not provide one. This helps maintain
consistent data and simplifies data entry.
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int DEFAULT 18
);
Explanation: Here, if no value is provided for AGE during an insert, the default value
of 18 will be assigned automatically.
How to Specify Constraints in SQL
Constraints can be specified during the table creation process using the CREATE
TABLE statement. Additionally, constraints can be modified or added to existing
tables using the ALTER TABLE statement.
Syntax for Creating Constraints:
CREATE TABLE table_name
(
column1 data_type [constraint_name],
column2 data_type [constraint_name],
column3 data_type [constraint_name],
...
);
We can also add or remove constraints after a table is created:
Example to Add a Constraint:
ALTER TABLE Student
ADD CONSTRAINT unique_student_id UNIQUE (ID);
Conclusion
SQL constraints are essential for maintaining data integrity and ensuring
consistency in relational databases. Understanding and implementing these
constraints effectively will help in designing robust, error-free databases. By
leveraging NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT,
and INDEX, you can ensure your database is optimized
for accuracy and performance.
Questions:
1. What is the difference between CHAR and VARCHAR2 data types?
CHAR: Fixed-length storage. If the defined length is not fully used, it is padded with spaces.
VARCHAR2: Variable-length storage. Only the actual data is stored, saving space when
the full length is not needed.
11. What are the differences between SQL and NoSQL databases?
SQL Databases:
Use structured tables with rows and columns.
Rely on a fixed schema.
Offer ACID properties.
NoSQL Databases:
Use flexible, schema-less structures (e.g., key-value pairs, document stores).
Are designed for horizontal scaling.
Often focus on performance and scalability over strict consistency.
12. What is a trigger in SQL?
A trigger is a set of SQL statements that automatically execute in response to
certain events on a table, such as INSERT, UPDATE, or DELETE. Triggers help
maintain data consistency, enforce business rules, and implement complex
integrity constraints.
19. What is the difference between the NVL and NVL2 functions?
NVL(): Replaces a NULL value with a specified replacement
value. Example: NVL(Salary, 0) will replace NULL with 0.
NVL2(): Evaluates two arguments:
If the first argument is NOT NULL, returns the second argument.
If the first argument is NULL, returns the third argument.
Example:
SELECT NVL(Salary, 0) AS AdjustedSalary FROM Employees; -- Replaces NULL with 0
If two employees have the same salary, they get the same rank, but RANK() will
skip a number for the next rank, while DENSE_RANK() will not.
23. What are window functions, and how are they used?
Window functions allow you to perform calculations across a set of table rows
that are related to the current row within a result set, without collapsing the
result set into a single row. These functions can be used to compute running
totals, moving averages, rank rows, etc.
Example: Calculating a running total
SELECT Name, Salary,
SUM(Salary) OVER (ORDER BY Salary) AS RunningTotal
FROM Employees;
The index on LastName lets the database quickly find all rows matching ‘Smith’
without scanning every record.
27. What is a materialized view, and how does it differ from a standard
view?
Standard View:
A virtual table defined by a query.
Does not store data; the underlying query is executed each time the view is
referenced.
A standard view shows real-time data.
Materialized View:
A physical table that stores the result of the query.
Data is precomputed and stored, making reads faster.
Requires periodic refreshes to keep data up to date.
materialized view is used to store aggregated sales data, updated nightly, for fast
reporting.
30. How can you handle duplicates in a query without using DISTINCT?
1. GROUP BY: Aggregate rows to eliminate duplicates
SELECT Column1, MAX(Column2)
FROM TableName
GROUP BY Column1;
2. ROW_NUMBER(): Assign a unique number to each row and filter by that
WITH CTE AS (
SELECT Column1, Column2, ROW_NUMBER() OVER (PARTITION BY Column1 ORDER
BY Column2) AS RowNum
FROM TableName
)
SELECT * FROM CTE WHERE RowNum = 1;
31. What are the best practices for writing optimized SQL queries?
1. Write Simple, Clear Queries:
Avoid overly complex joins and subqueries.
Use straightforward, well-structured SQL that is easy to read and maintain.
2. Filter Data Early:
Apply WHERE clauses as early as possible to reduce the amount of data
processed.
Consider using indexed columns in WHERE clauses for faster lookups.
3. **Avoid SELECT *:
Retrieve only the columns needed. This reduces I/O and improves performance.
4. Use Indexes Wisely:
Create indexes on columns that are frequently used in WHERE clauses, JOIN
conditions, and ORDER BY clauses.
Regularly review index usage and remove unused indexes.
5. Leverage Query Execution Plans:
Use execution plans to identify bottlenecks, missing indexes, or inefficient query
patterns.
6. Use Appropriate Join Types:
Choose INNER JOIN, LEFT JOIN, or OUTER JOIN based on the data relationships
and performance requirements.
7. Break Down Complex Queries:
Instead of a single monolithic query, use temporary tables or CTEs to process data
in stages.
8. Optimize Aggregations:
Use GROUP BY and aggregate functions efficiently.
Consider pre-aggregating data if queries frequently require the same
computations.
9. Monitor Performance Regularly:
Continuously analyze query performance and fine-tune as data volumes grow or
usage patterns change.
32. What is the purpose of the SQL PIVOT operator?
The PIVOT operator transforms rows into columns, making it easier to summarize or
rearrange data for reporting.
Example:
Converting a dataset that lists monthly sales into a format that displays each month as a
separate column.
SELECT ProductID, [2021], [2022]
FROM (
SELECT ProductID, YEAR(SaleDate) AS SaleYear, Amount
FROM Sales
) AS Source
PIVOT (
SUM(Amount)
FOR SaleYear IN ([2021], [2022])
) AS PivotTable;
Explanation:
This query fetches details of employees whose salary exceeds the average salary.
The subquery calculates the average salary, and the main query filters rows based
on that result.
34. Write a query to fetch the duplicate values from a column in a table.
SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName
HAVING COUNT(*) > 1;
Explanation:
The query uses GROUP BY to group identical values and HAVING COUNT(*) > 1 to
identify values that appear more than once in the specified column.
84. Write a query to find the employees who joined in the last 30 days.
SELECT *
FROM Employee
WHERE JoiningDate > DATE_SUB(CURDATE(), INTERVAL 30 DAY);
Explanation:
By comparing the JoiningDate to the current date minus 30 days, this query retrieves
all employees who joined within the last month.
35. Write a query to fetch top 3 earning employees.
SELECT *
FROM Employee
ORDER BY Salary DESC
LIMIT 3;
Explanation:
The query sorts employees by salary in descending order and uses LIMIT 3 to
return only the top three earners.
36. Write a query to delete duplicate rows in a table without using the ROWID
keyword.
DELETE FROM Employee
WHERE EmployeeID NOT IN (
SELECT MIN(EmployeeID)
FROM Employee
GROUP BY Column1, Column2
);
Explanation:
This query retains only one row for each set of duplicates by keeping the row with
the smallest EmployeeID. It identifies duplicates using GROUP BY and removes rows
not matching the minimum ID.
87. Write a query to fetch common records from two tables.
SELECT *
FROM TableA
INNER JOIN TableB ON TableA.ID = TableB.ID;
Explanation:
An INNER JOIN is used to find rows present in both tables by matching a common
column (in this case, ID).
37. Write a query to fetch employees whose names start and end with ‘A’.
SELECT *
FROM Employee
WHERE Name LIKE 'A%' AND Name LIKE '%A';
Explanation:
The query uses LIKE with wildcard characters to filter rows where the Name column
starts and ends with the letter 'A'.
89. Write a query to display all departments along with the number of employees in each.
SELECT DepartmentID, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY DepartmentID;
Explanation:
By grouping employees by their DepartmentID and counting rows in each group, the
query produces a list of departments along with the employee count.
90. Write a query to find employees who do not have managers.
SELECT *
FROM Employee
WHERE ManagerID IS NULL;
Explanation:
This query selects employees whose ManagerID column is NULL, indicating they
don’t report to a manager.
38. Write a query to fetch the 3rd and 4th highest salaries.
WITH SalaryRank AS (
SELECT Salary, RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM Employee
)
SELECT Salary
FROM SalaryRank
WHERE Rank IN (3, 4);
Explanation:
This query uses the RANK() window function to rank the salaries in descending
order. The outer query then selects the 3rd and 4th highest salaries by filtering for
those ranks.
39. Write a query to transpose rows into columns.
SELECT
MAX(CASE WHEN ColumnName = 'Condition1' THEN Value END) AS Column1,
MAX(CASE WHEN ColumnName = 'Condition2' THEN Value END) AS Column2
FROM TableName;
Explanation:
This query converts specific row values into columns using conditional
aggregation with CASE. Each column’s value is determined based on a condition
applied to rows.
40. Write a query to fetch records updated within the last hour.
SELECT *
FROM TableName
WHERE UpdatedAt >= NOW() - INTERVAL 1 HOUR;
Explanation:
By comparing the UpdatedAt timestamp to the current time minus one hour, the
query retrieves rows updated in the last 60 minutes.
41. Write a query to list employees in departments that have fewer than 5
employees.
SELECT *
FROM Employee
WHERE DepartmentID IN (
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
HAVING COUNT(*) < 5
);
Explanation:
The subquery counts employees in each department, and the main query uses
those results to find employees working in departments with fewer than 5
members.
42. Write a query to check if a table contains any records.
SELECT CASE
WHEN EXISTS (SELECT * FROM TableName) THEN 'Has Records'
ELSE 'No Records'
END AS Status;
Explanation:
The query uses EXISTS to determine if any rows exist in the table, returning a
status of 'Has Records' or 'No Records' based on the result.
43. Write a query to find employees whose salaries are higher than their
managers.
SELECT e.EmployeeID, e.Salary
FROM Employee e
JOIN Employee m ON e.ManagerID = m.EmployeeID
WHERE e.Salary > m.Salary;
Explanation:
This query joins the Employee table with itself to compare employee salaries to
their respective managers’ salaries, selecting those who earn more.
44. Write a query to fetch alternating rows from a table.
WITH RowNumbered AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum
FROM Employee
)
SELECT *
FROM RowNumbered
WHERE RowNum % 2 = 0;
Explanation:
This query assigns a sequential number to each row using ROW_NUMBER(), then
selects rows where the row number is even, effectively fetching alternating rows.
The ORDER BY (SELECT NULL) is used to avoid any specific ordering and just apply a
sequential numbering.
45. Write a query to find departments with the highest average salary.
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
ORDER BY AVG(Salary) DESC
LIMIT 1;
Explanation:
Grouping by DepartmentID and ordering by the average salary in descending order,
the query returns the department with the highest average.
46. Write a query to fetch the nth record from a table.
WITH OrderedEmployees AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum
FROM Employee
)
SELECT *
FROM OrderedEmployees
WHERE RowNum = n;
Explanation:
This query uses ROW_NUMBER() to generate a sequential number for each row.
The outer query then retrieves the row where the number matches the
desired nth position. The approach is portable across most databases.
47. Write a query to find employees hired in the same month of any year.
SELECT *
FROM Employee
WHERE MONTH(JoiningDate) = MONTH(CURDATE());
Explanation:
By comparing the month of JoiningDate to the current month, the query selects all
employees who were hired in that month regardless of the year.