SQL Notes
SQL Notes
SQL (Structured Query Language) is a standardized programming language used for managing and
manipulating relational databases. It allows users to perform various operations such as creating,
reading, updating, and deleting data stored in databases. SQL is essential for interacting with
relational database management systems (RDBMS) like MySQL, PostgreSQL, Microsoft SQL Server,
Oracle Database, and SQLite.
2. Real-Time Querying: It allows applications to fetch or modify data instantly, crucial for
dynamic systems like websites, e-commerce, and financial applications.
3. Data Analysis: It helps in extracting insights from data by performing aggregations, joins, and
filtering.
4. Interoperability: SQL can integrate with various programming languages and tools, enabling
seamless interaction with databases.
5. Security and Compliance: SQL supports user authentication, permissions, and data
encryption, ensuring secure data handling.
1. Web Development
Backend Database Management: Websites like e-commerce platforms use SQL to store and
retrieve user profiles, product information, and transactions.
Dynamic Content Delivery: SQL queries are used to display personalized content (e.g.,
search results, recommendations).
2. Data Analytics
Data Warehousing: SQL queries are used to extract, transform, and load (ETL) data for
analysis.
Business Intelligence (BI): Tools like Tableau and Power BI use SQL to connect to databases
and generate reports.
Data Mining: Analysts use SQL to discover patterns and trends in data.
3. Financial Applications
Fraud Detection: Complex queries identify anomalies and patterns indicating fraudulent
activities.
4. Healthcare
Patient Records Management: Stores and retrieves electronic medical records (EMRs)
efficiently.
Clinical Data Analysis: SQL helps analyze patient data for research or to improve healthcare
services.
Citizen Data Management: SQL is used to manage census data, tax records, and land
registries.
Election Systems: SQL databases store voter details and election results.
6. E-commerce
Inventory Management: SQL manages product inventory, stock levels, and supplier data.
Order Processing: SQL queries track orders, payments, and shipping statuses.
7. Education
Learning Management Systems (LMS): SQL handles data like course details, student records,
and exam results.
Research: Universities use SQL for storing and analyzing research data.
8. Mobile Applications
Local Storage: Apps like WhatsApp use SQLite, a lightweight SQL database, to store data
locally.
9. Gaming
Game State Management: Tracks in-game progress and interactions in multiplayer games.
User Profiles: Manages data like posts, comments, likes, and friend connections.
Recommendation Engines: SQL queries drive algorithms to suggest content based on user
behavior.
Logistics: Used for managing shipping routes, inventory distribution, and order statuses.
Device Data Storage: SQL stores data from IoT devices like sensors and cameras.
Monitoring Systems: Queries analyze real-time data for alerts and decision-making.
Data Preprocessing: SQL is used to clean and prepare data for training ML models.
SQL powers CRM tools like Salesforce, storing data about customer interactions, leads, and
campaigns.
Employee Records: SQL is used for managing employee data like salaries, attendance, and
performance.
Data Lakes and Warehouses: SQL is integrated into big data tools like Hadoop (Hive SQL) and
cloud platforms like Google BigQuery, AWS Redshift, and Azure Synapse for querying massive
datasets.
Real-Time Analytics: SQL is used in systems like Apache Kafka (via KSQL) for processing
streaming data.
Configuration Management: SQL is employed in tools like Puppet or Ansible for managing
database configurations.
Database Performance Monitoring: Tools like SQL Profiler and New Relic use SQL to monitor
query performance and database health.
CI/CD Pipelines: SQL scripts are automated in deployment pipelines to ensure databases are
updated alongside application code.
Search Optimization: SQL is used for storing and retrieving data that powers chatbots and
NLP systems.
Semantic Analysis: SQL queries enable preprocessing of structured text data for further
analysis in AI systems.
19. Energy Sector
Smart Grids: SQL is used for analyzing data collected from smart meters and optimizing
energy usage.
Resource Tracking: Manages fuel supply chains and power distribution data.
Route Optimization: SQL stores and queries large geospatial datasets, aiding in real-time
route planning.
Fleet Management: SQL-powered systems track vehicle locations, fuel usage, and
maintenance schedules.
21. Cybersecurity
Threat Analysis: SQL is used for querying logs and identifying suspicious patterns.
Access Control: Databases implement SQL to enforce role-based access control and monitor
login attempts.
Transaction Tracking: SQL databases are often used in hybrid blockchain systems for storing
off-chain data.
Wallet Management: Applications use SQL to keep track of balances and transaction
histories.
Property Listings: SQL manages large datasets of properties, pricing, and customer
information.
Market Analysis: Aggregates data for generating insights into property trends and valuations.
Content Streaming: SQL is used in platforms like Netflix and Spotify for catalog management
and user data retrieval.
Ad Campaigns: Queries help target specific audiences and analyze campaign effectiveness.
25. Manufacturing
Production Planning: SQL stores and retrieves schedules, machine data, and workforce
information.
Quality Control: Stores test results and compares them with standards for quality assurance.
Weather Data Analysis: SQL is used for storing and analyzing meteorological data for
forecasting.
Wildlife Conservation: Tracks animal movements and population statistics using geospatial
SQL queries.
27. Augmented Reality (AR) and Virtual Reality (VR)
Spatial Data Management: SQL supports storing 3D spatial data for immersive applications.
28. Insurance
Policy Management: SQL handles details like customer information, policy terms, and claims.
Risk Assessment: Aggregates historical data to identify risk factors and calculate premiums.
29. Telecommunications
Call Records: Stores and retrieves call detail records (CDRs) for billing and analysis.
Network Optimization: SQL is used to analyze bandwidth usage and predict future needs.
Genomics: SQL manages large-scale genomic datasets for analyzing DNA sequences.
Physics Simulations: Stores simulation outputs and experimental data for validation.
Real-Time Updates: Multiplayer games use SQL to manage leaderboards, match histories,
and achievements.
Databases
A database is an organized collection of data that is stored and managed so it can be easily accessed,
retrieved, updated, and used. Databases are used in almost every application today to store,
manage, and analyze data.
o Data is stored in tables (rows and columns) and follows a structured schema.
o Examples:
MySQL
PostgreSQL
Oracle Database
Microsoft SQL Server
2. NoSQL Database
o Subtypes:
Graph Databases: Store data as nodes and edges, ideal for relational data.
3. Cloud Database
o Examples:
4. Distributed Database
5. Object-oriented Database
7. Network Database
8. Time-series Database
9. Columnar Database
Summary Table
2. Under MySQL Community Downloads, select MySQL Installer for Windows (if you're using
Windows) or choose the appropriate version for your operating system.
o Username: root.
Example:
SELECT VERSION();
SQL (Structured Query Language) is a standard programming language used to interact with
relational databases. It allows users to create, manipulate, and query data efficiently.
o Commands:
TRUNCATE: Deletes all records from a table but retains the structure.
o Commands:
Example: INSERT INTO Employees (ID, Name, Age) VALUES (1, 'John',
30);
o Command:
Example: SELECT Name, Age FROM Employees WHERE Age > 25;
o Commands:
o Commands:
Example: COMMIT;
Example: ROLLBACK;
SQL data types define the kind of data that can be stored in a table's column.
o Common types:
o Common types:
o Common types:
Applications:
SQL example using all the command types (DDL, DML, DQL, DCL, and TCL). We'll create a dataset of
employees working in a company.
Create a Table:
sql
Copy code
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
JoiningDate DATE
);
sql
Copy code
Drop a Column:
sql
Copy code
Insert Data:
Here are the first few records (you can repeat similarly for 50 records):
sql
Copy code
VALUES
Update Data:
sql
Copy code
UPDATE Employees
Delete Data:
sql
Copy code
sql
Copy code
sql
Copy code
sql
Copy code
Order Results:
sql
Copy code
Aggregate Function:
sql
Copy code
Grant Permission:
sql
Copy code
Revoke Permission:
sql
Copy code
Start a Transaction:
sql
Copy code
BEGIN TRANSACTION;
ROLLBACK;
sql
Copy code
BEGIN TRANSACTION;
COMMIT;
DQL commands and operators using the Employees dataset we created earlier. Each operator is
accompanied by examples for clarity.
1. SELECT
The SELECT statement is used to retrieve data from one or more tables.
Example:
sql
Copy code
sql
Copy code
2. LIMIT
Example:
sql
Copy code
Example:
sql
Copy code
4. WHERE Clause
Example:
sql
Copy code
5. AND Operator
Example:
sql
Copy code
SELECT * FROM Employees WHERE Department = 'IT' AND Salary > 60000;
6. OR Operator
Example:
Copy code
7. IN Operator
Example:
sql
Copy code
8. NOT IN Operator
Example:
sql
Copy code
9. BETWEEN
Example:
sql
Copy code
sql
Copy code
10. EXISTS
Example:
Get employees who belong to departments where at least one employee earns more than 70,000.
sql
Copy code
WHERE EXISTS (
SELECT 1 FROM Employees e2 WHERE [Link] > 70000 AND [Link] = [Link]
);
11. IS NULL
Example:
sql
Copy code
Example:
Copy code
13. Wildcards
Examples:
14. ORDER BY
Examples:
how to use CASE WHEN THEN for logical problem-solving and handle NULL values with IFNULL and
COALESCE in SQL. We will create a sample dataset and demonstrate these concepts.
sql
Copy code
StudentID INT,
Name VARCHAR(50),
Subject VARCHAR(50),
Marks INT
);
Marks: Marks obtained by the student. Some students have NULL marks.
Problem:
Query:
sql
Copy code
SELECT
StudentID,
Name,
Subject,
Marks,
CASE
END AS Performance
FROM StudentGrades;
Explanation:
If a condition matches, it stops evaluating further and returns the corresponding value.
Problem:
sql
Copy code
SELECT
StudentID,
Name,
Subject,
FROM StudentGrades;
sql
Copy code
SELECT
StudentID,
Name,
Subject,
FROM StudentGrades;
Explanation:
Problem:
Classify students' performance after substituting NULL Marks with a default value.
Query:
sql
Copy code
SELECT
StudentID,
Name,
Subject,
CASE
END AS Performance
FROM StudentGrades;
Explanation:
Use COALESCE to handle NULL values before passing them into the CASE statement.
Output Example:
the concepts of Group Operations and Aggregate Functions in SQL with a detailed explanation,
followed by example queries using a dataset.
Concepts Explained
1. GROUP BY:
o The GROUP BY clause groups rows that have the same values into summary rows.
o It is often used with aggregate functions like COUNT, SUM, AVG, MIN, MAX, etc., to
produce a summary of data.
2. HAVING Clause:
o The HAVING clause is used to filter records after the GROUP BY operation.
o Unlike WHERE, which filters rows before aggregation, HAVING is applied to the
grouped data.
3. COUNT:
o The COUNT function returns the number of rows that match a specified condition.
4. SUM:
5. AVG:
6. MIN:
7. MAX:
o COUNT can also be used with specific columns to count non-NULL values in that
column, including strings.
Let's create a sample SalesData table with various fields that will help in demonstrating these
concepts:
sql
Copy code
SaleID INT,
ProductName VARCHAR(50),
SaleDate DATE,
Quantity INT,
Region VARCHAR(50)
);
INSERT INTO SalesData (SaleID, ProductName, SaleDate, Quantity, UnitPrice, Region) VALUES
sql
Copy code
FROM SalesData
GROUP BY ProductName;
Explanation:
This groups the data by ProductName and calculates the total quantity sold for each product.
Output:
ProductName TotalQuantity
Laptop 6
Smartphone 12
Tablet 3
Example 2: Group by Region and Filter for Regions with Total Sales Greater Than 5
sql
Copy code
FROM SalesData
GROUP BY Region
The HAVING clause is used to filter the groups after the aggregation. Here, it filters out
regions where the total quantity is 5 or less.
Output:
Regio TotalQuantity
n
North 7
South 12
3. COUNT Function
sql
Copy code
FROM SalesData
GROUP BY ProductName;
Explanation:
The COUNT function counts the number of sales for each product by counting the SaleID.
Output:
ProductName NumberOfSales
Laptop 3
Smartphone 3
Tablet 2
4. SUM Function
Example 4: Calculate the Total Revenue for Each Product (Quantity * UnitPrice)
sql
Copy code
FROM SalesData
GROUP BY ProductName;
Explanation:
This query multiplies the Quantity by UnitPrice to calculate the revenue for each product and
then sums it up.
Output:
ProductNam TotalRevenue
e
Laptop 6000.00
Smartphone 6000.00
Tablet 900.00
5. AVG Function
sql
Copy code
FROM SalesData
GROUP BY ProductName;
Explanation:
The AVG function calculates the average quantity sold for each product.
Output:
ProductNam AverageQuantity
e
Laptop 2.00
Smartphone 4.00
Tablet 1.50
Example 6: Find the Minimum and Maximum Unit Price for Each Product
sql
Copy code
FROM SalesData
GROUP BY ProductName;
Explanation:
MIN and MAX functions return the minimum and maximum prices for each product.
Output:
Example 7: Count the Number of Sales for Each Region (excluding NULL)
sql
Copy code
FROM SalesData
GROUP BY Region;
Explanation:
This counts the distinct products sold in each region, excluding NULL values in ProductName.
Output:
Region ProductCount
North 2
South 2
East 1
West 2
sql
Copy code
FROM SalesData
Explanation:
YEAR() extracts the year from SaleDate to count the sales made in the year 2024.
Output:
SalesIn2024
Final Thoughts:
Aggregate functions like COUNT, SUM, AVG, MIN, and MAX help summarize data.
Date & Time Functions allow manipulation and filtering based on dates.
=======================================================================
1. NOT NULL
2. UNIQUE
3. CHECK
4. DEFAULT
5. PRIMARY KEY
2. The Employees table will contain employee details and have a foreign key reference to
Departments.
We will create the Departments table with a PRIMARY KEY constraint on DepartmentID, a NOT NULL
constraint on DepartmentName, and a UNIQUE constraint for department names.
sql
Copy code
DepartmentID INT PRIMARY KEY, -- PRIMARY KEY constraint: ensures each department has a
unique identifier
DepartmentName VARCHAR(50) NOT NULL, -- NOT NULL constraint: ensures that the department
name cannot be empty
);
Explanation:
PRIMARY KEY: The DepartmentID column is the primary key, meaning each department has
a unique identifier, and no two departments can have the same DepartmentID.
NOT NULL: The DepartmentName cannot be NULL, ensuring every department must have a
name.
Copy code
CHECK constraint for the Age column to ensure the employee is at least 18 years old.
DEFAULT constraint for HireDate to automatically set the date of hire if not provided.
sql
Copy code
EmployeeID INT PRIMARY KEY, -- PRIMARY KEY: ensures each employee has a unique
identifier
FirstName VARCHAR(50) NOT NULL, -- NOT NULL: ensures First Name cannot be empty
LastName VARCHAR(50) NOT NULL, -- NOT NULL: ensures Last Name cannot be empty
Age INT CHECK (Age >= 18), -- CHECK: ensures Age is at least 18
Email VARCHAR(100) UNIQUE, -- UNIQUE: ensures that each email address is unique
HireDate DATE DEFAULT CURRENT_DATE, -- DEFAULT: sets the hire date to the current date if not
provided
);
Explanation:
PRIMARY KEY: The EmployeeID column is the primary key for this table. It ensures each
employee has a unique ID.
NOT NULL: FirstName and LastName columns cannot contain NULL values.
CHECK: The Age column has a check constraint, ensuring that employees must be at least 18
years old.
UNIQUE: The Email column ensures that no two employees can have the same email
address.
DEFAULT: The HireDate column will automatically take the current date if no value is
provided when inserting a new employee.
FOREIGN KEY: The DepartmentID in the Employees table refers to DepartmentID in the
Departments table, establishing a relationship between the two tables. This ensures that
employees are assigned to valid departments.
sql
Copy code
INSERT INTO Employees (EmployeeID, FirstName, LastName, Age, Email, DepartmentID) VALUES
1. NOT NULL:
Example:
sql
Copy code
-- This will throw an error because LastName is NULL
3. UNIQUE:
o The Email column in the Employees table ensures that each email is unique.
Example:
sql
Copy code
-- This will throw an error because the email already exists
4.
5. CHECK:
o The Age column in the Employees table ensures the value must be 18 or older.
Example:
sql
Copy code
-- This will throw an error because Age is less than 18
6.
7. DEFAULT:
o If we don't specify a HireDate when inserting a record into the Employees table, it
will automatically use the current date.
Example:
sql
Copy code
-- The HireDate will automatically be set to the current date
8.
9. PRIMARY KEY:
o The EmployeeID column in the Employees table uniquely identifies each employee.
This means that no two employees can have the same EmployeeID.
Example:
sql
Copy code
-- This will throw an error because EmployeeID 1 already exists
INSERT INTO Employees (EmployeeID, FirstName, LastName, Age, Email, DepartmentID)
10.
Example:
sql
Copy code
-- This will throw an error because DepartmentID 5 does not exist in the Departments table
12.
Departments Table:
Employees Table:
Summary of Constraints:
FOREIGN KEY: Enforces referential integrity by linking one table's column to another table's
primary key.
Joins are used to combine rows from two or more tables based on a related column between them.
Let's create a dataset and explain the different types of joins with examples.
Example Dataset
sql
Copy code
DepartmentName VARCHAR(50)
);
FirstName VARCHAR(50),
LastName VARCHAR(50),
DepartmentID INT,
);
sql
Copy code
(1, 'Sales'),
(2, 'HR'),
(3, 'Engineering');
1. INNER JOIN
Purpose: Returns rows when there is a match in both tables. If there is no match, the row is
excluded from the result.
Example:
sql
Copy code
FROM Employees
This query returns employees who are assigned to a department. If an employee does not
belong to a department (e.g., Diana), they are not included in the result.
Result:
2 Bob Johnson HR
Purpose: Returns all rows from the left table (the first table) and the matched rows from the
right table (the second table). If there is no match, NULL values are returned for columns
from the right table.
Example:
sql
Copy code
FROM Employees
Explanation:
This query returns all employees, even if they are not assigned to any department (e.g.,
Diana will still appear, but the department column will be NULL).
Result:
Purpose: Returns all rows from the right table (the second table) and the matched rows from
the left table (the first table). If there is no match, NULL values are returned for columns
from the left table.
Example:
sql
Copy code
FROM Employees
Explanation:
Result:
2 Bob Johnson HR
4. CROSS JOIN
Purpose: Returns the Cartesian product of the two tables, i.e., every row from the first table
is combined with every row from the second table. This can result in a large number of
results, depending on the size of the tables.
Example:
sql
Copy code
FROM Employees
Explanation:
This query will return all combinations of employees and departments. It does not require a
join condition and creates every possible pair between employees and departments.
Result:
Alice Smith HR
Bob Johnson HR
Charlie Brown HR
Diana Clark HR
Diana Clark Engineering
5. SELF JOIN
Purpose: A self-join is a regular join but the table is joined with itself. It’s used when you
need to compare rows within the same table.
Example:
sql
Copy code
FROM Employees E1
Explanation:
This query assumes that employees in the same department have a manager (in this
example, we just use LEFT JOIN on the same table to find managers). It compares each
employee to others within the same department.
Result:
1 Alice Charlie
2 Bob NULL
3 Charlie Alice
4 Diana NULL
Purpose: Combines the result of both a LEFT JOIN and a RIGHT JOIN. It returns all rows from
both tables. When there is no match, the result is NULL on the side that doesn't have a
match.
Example:
sql
Copy code
FROM Employees
Explanation:
This query returns all rows from both Employees and Departments. If there is no match, the
result will show NULL for the missing side (e.g., employees without a department, or
departments without employees).
Result:
2 Bob Johnson HR
Summary of Joins:
2. LEFT JOIN: Returns all rows from the left table and matching rows from the right table. Non-
matching rows in the right table will have NULL.
3. RIGHT JOIN: Returns all rows from the right table and matching rows from the left table.
Non-matching rows in the left table will have NULL.
4. CROSS JOIN: Returns all possible combinations of rows between two tables (Cartesian
product).
6. FULL OUTER JOIN: Returns all rows from both tables, with NULL for non-matching rows from
either side.
DDL (Data Definition Language)
DDL commands are used to define and manage database structures like tables, schemas, and
constraints. These commands do not manipulate the data itself but define the structure of the
database. The main DDL commands include:
1. CREATE
2. DROP
3. ALTER
4. RENAME
5. TRUNCATE
6. MODIFY
7. COMMENT
1. CREATE
Purpose: The CREATE command is used to create a new table, view, index, or other objects in
the database.
sql
Copy code
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT
);
Explanation:
This query creates a new table named Employees with columns EmployeeID, FirstName,
LastName, and Age. EmployeeID is the primary key, meaning it must be unique for each
employee.
2. DROP
Purpose: The DROP command is used to delete an existing table, view, or other database
objects, along with all its data. Be cautious with DROP, as it removes the structure and data
permanently.
sql
Copy code
Explanation:
This command deletes the Employees table along with all its rows and structure. Once
dropped, the table and its data cannot be recovered unless a backup exists.
3. ALTER
Purpose: The ALTER command is used to modify an existing database object, such as adding,
deleting, or modifying columns in a table, changing a column's data type, or adding
constraints.
sql
Copy code
Explanation:
This command adds a new column named Email with a data type of VARCHAR(100) to the
Employees table.
sql
Copy code
Explanation:
This command modifies the Age column in the Employees table to ensure it cannot have
NULL values.
4. RENAME
sql
Copy code
Explanation:
Example: Rename a Column (Note: Not all SQL versions support this directly)
sql
Copy code
Explanation:
This command renames the Age column in the Employees table to YearsOld.
5. TRUNCATE
Purpose: The TRUNCATE command is used to remove all rows from a table without deleting
the table structure. It is faster than DELETE as it does not log individual row deletions.
sql
Copy code
Explanation:
This command removes all data from the Employees table but leaves the table structure
intact. It cannot be rolled back in most database systems.
Purpose: The MODIFY command is used to change the properties of an existing column, such
as its data type or constraints.
Example: Modify a Column to Change Its Data Type
sql
Copy code
Explanation:
This command changes the data type of the Age column from INT to BIGINT to allow larger
values.
7. COMMENT
sql
Copy code
sql
Copy code
Explanation:
The first command adds a comment to the Employees table, explaining that it stores
employee information.
The second command adds a comment to the Age column, explaining that it stores the
employee's age.
5. TRUNCATE: Removes all rows from a table but keeps its structure.
6. MODIFY: Changes the properties of an existing column (often used with ALTER).
DML (Data Manipulation Language) & TCL (Transaction Control Language) Commands
DML commands are used to manipulate the data within the tables (i.e., to insert, update, or delete
data). TCL commands control the transaction behavior, such as committing, rolling back, or setting
savepoints for data transactions.
1. INSERT
Purpose: The INSERT command is used to add new rows of data to a table.
Example:
sql
Copy code
Explanation:
This command inserts a new employee record into the Employees table with the specified
EmployeeID, FirstName, LastName, Age, and DepartmentID. The DepartmentID refers to an
existing department.
2. UPDATE
Purpose: The UPDATE command is used to modify the existing records in a table.
Example:
sql
Copy code
SET Age = 29
WHERE EmployeeID = 1;
Explanation:
This query updates the Age of the employee whose EmployeeID is 1. The WHERE clause
ensures that only the relevant row is updated.
3. DELETE
Purpose: The DELETE command is used to remove one or more rows from a table.
Example:
sql
Copy code
WHERE EmployeeID = 1;
Explanation:
This command deletes the employee with EmployeeID 1 from the Employees table. The
WHERE clause is used to ensure that only the targeted employee is deleted. Without a
WHERE clause, all rows will be deleted.
TCL commands are used to manage the changes made by DML commands within transactions. A
transaction is a logical unit of work that contains one or more DML operations, and TCL ensures data
consistency and rollback.
1. COMMIT
Purpose: The COMMIT command is used to save all changes made in the current transaction
permanently. Once committed, the changes cannot be rolled back.
Example:
sql
Copy code
Explanation:
This command finalizes the transaction and saves all the changes made during that
transaction. After a COMMIT, the changes are permanent and will persist even if the session
is closed.
2. ROLLBACK
Purpose: The ROLLBACK command is used to undo the changes made during the current
transaction. It reverts the data to its state before the transaction started.
Example:
sql
Copy code
ROLLBACK;
Explanation:
If a transaction is in progress and something goes wrong, ROLLBACK undoes any changes
made during that transaction, reverting the database to its previous state.
3. SAVEPOINT
Purpose: The SAVEPOINT command is used to set a point within a transaction to which you
can later roll back without affecting the entire transaction. This allows partial rollbacks.
Example:
sql
Copy code
SAVEPOINT Savepoint1;
Explanation:
Data Partitioning
Data Partitioning refers to the process of splitting large tables into smaller, more manageable pieces
called partitions. This can improve query performance, make it easier to manage large datasets, and
enhance data availability. Partitioning is often used in databases where large amounts of data need
to be stored, queried, and managed efficiently.
Range Partitioning: Divides data into ranges based on a column, such as dates or numerical
values.
List Partitioning: Divides data into groups based on distinct values of a column.
Hash Partitioning: Distributes data evenly across multiple partitions using a hash function.
sql
Copy code
SaleID INT,
SaleDate DATE,
Amount DECIMAL(10, 2)
);
Explanation:
In this example, the Sales table is partitioned by the SaleDate column. Sales from the year
2023 will be in p2023, and sales from 2024 will be in p2024. This partitioning helps to query
sales from specific years more efficiently.
DML:
TCL:
3. SAVEPOINT: Sets a point in the transaction to which you can roll back without affecting the
entire transaction.
Data Partitioning:
Divides large tables into smaller, more manageable partitions to improve performance and
data management.
=======================================================================
Indexes and Views are essential database objects in SQL that help improve performance and simplify
complex queries. Additionally, Stored Procedures allow for encapsulating logic into reusable blocks,
making database operations more efficient and manageable.
Indexes
An index is a database object that improves the speed of data retrieval operations on a table at the
cost of additional space and slower data modification operations (like INSERT, UPDATE, and DELETE).
Indexes are typically created on columns that are frequently queried.
Types of Indexes
3. Unique Index: Ensures that the values in the indexed column(s) are unique.
6. Non-clustered Index: Does not alter the physical order of the data and allows multiple non-
clustered indexes to be created.
sql
Copy code
Explanation:
This query creates a non-clustered index named idx_lastname on the LastName column of
the Employees table to speed up queries that filter by last name.
sql
Copy code
Explanation:
This query creates an index on both the FirstName and LastName columns, which is useful
for queries that filter by both columns.
Views in SQL
A view is a virtual table created by a query that selects data from one or more tables. Views do not
store data physically; they only store the query definition. They are helpful for simplifying complex
queries, securing data, and presenting data in a specific format.
Types of Views
1. Simple View: Based on a single table and does not include any complex operations like joins
or subqueries.
2. Complex View: Based on multiple tables and can include joins, aggregations, or subqueries.
3. Materialized View: Stores the result of a query physically and can be refreshed periodically
(Not supported in all databases).
Copy code
FROM Employees
Explanation:
This creates a view named EmployeeDepartment that combines data from Employees and
Departments tables using a JOIN. This view simplifies querying the names of employees
along with their respective departments.
sql
Copy code
Explanation:
Stored Procedures
A stored procedure is a set of SQL statements that can be executed as a single unit. It allows for
reusing logic, improving performance by reducing the amount of SQL sent to the server, and provides
better control over transactions. Stored procedures can take parameters as input (IN), output (OUT),
or both (INOUT).
3. INOUT Parameter: Used to pass values to the procedure and return modified values.
An IN parameter is used to pass a value into the procedure. It can be used to filter or modify the
query behavior.
Example: Procedure with IN Parameter
sql
Copy code
BEGIN
FROM Employees
END;
Explanation:
sql
Copy code
CALL GetEmployeeByDepartment(2);
Explanation:
This command calls the GetEmployeeByDepartment procedure with dept_id set to 2 to get
employees in department 2.
An OUT parameter allows the procedure to return a value back to the caller. This is useful when you
need the procedure to return data like counts or status indicators.
sql
Copy code
BEGIN
FROM Employees
WHERE DepartmentID = dept_id;
END;
Explanation:
sql
Copy code
Explanation:
This calls the procedure for department 2 and stores the employee count in the
@emp_count variable, then selects the count.
An INOUT parameter allows the procedure to both receive a value and return a modified value. This
is useful when you want to modify a value within the procedure and return the modified value.
sql
Copy code
BEGIN
UPDATE Employees
SET emp_id = emp_id + 1; -- Modifying the emp_id to show the next employee ID
END;
Explanation:
This stored procedure UpdateEmployeeAge takes two parameters: emp_id (INOUT) and
new_age (IN). It updates the employee's age and also increments the emp_id by 1 before
returning it.
sql
Copy code
Explanation:
This calls the procedure to update the age of the employee with EmployeeID = 1 and
automatically modifies emp_id in the process.
1. Indexes: Improve data retrieval performance by creating a data structure that allows faster
searches.
2. Views: Virtual tables based on a query. Simplifies complex queries and enhances security.
3. Stored Procedures: Reusable blocks of SQL code that can accept parameters.
=======================================================================
SQL functions are built-in or user-defined routines that can be invoked to perform operations on
data. Window functions are advanced types of functions that allow users to perform calculations
across a set of table rows related to the current row. SQL also offers constructs like exceptions,
triggers, and subqueries to help you manage complex logic and handle specific conditions.
A User-Defined Function is a custom function created by the user to perform specific operations on
data. These functions can return a single value or a table (in the case of table-valued functions).
sql
Copy code
RETURNS VARCHAR(100)
BEGIN
INTO full_name
FROM Employees
RETURN full_name;
END;
Explanation:
This function, GetEmployeeFullName, takes an employee_id as input and returns the full
name of the employee (first and last name concatenated).
sql
Copy code
SELECT GetEmployeeFullName(1);
2. Window Functions
Window Functions allow you to perform calculations across a set of rows related to the current row.
They differ from aggregate functions as they do not collapse rows into a single result.
RANK()
DENSE_RANK()
LEAD()
LAG()
ROW_NUMBER()
Copy code
SaleID INT,
EmployeeID INT,
SaleDate DATE
);
RANK()
The RANK() function assigns a unique rank to each row within the partition of a result set. If there are
duplicate values, they receive the same rank, but the subsequent rank(s) will be skipped.
sql
Copy code
FROM Sales;
Explanation:
This query assigns a rank to each employee based on the SaleAmount in descending order.
DENSE_RANK()
The DENSE_RANK() function is similar to RANK(), but without gaps between ranks for duplicate
values.
sql
Copy code
FROM Sales;
Explanation:
This assigns a dense rank, meaning no gaps in ranking, even for rows with equal values.
sql
Copy code
FROM Sales;
Explanation:
The LEAD() function gets the next sale amount, while the LAG() function retrieves the
previous sale amount.
ROW_NUMBER()
The ROW_NUMBER() function assigns a unique number to each row, starting from 1 for the first row
in each partition.
sql
Copy code
FROM Sales;
Explanation:
This function generates a sequential number for each row based on the SaleAmount.
UNION
The UNION operator combines the results of two or more SELECT statements and removes
duplicates.
sql
Copy code
UNION
SELECT FirstName FROM Managers;
Explanation:
UNION ALL
The UNION ALL operator combines the results of two or more SELECT statements but does not
remove duplicates.
sql
Copy code
UNION ALL
Explanation:
INTERSECT
The INTERSECT operator returns only the rows that appear in both result sets.
sql
Copy code
INTERSECT
Explanation:
Returns the names that appear in both the Employees and Managers tables.
A subquery is a query within another query, typically used to return values that are used by the
outer query.
sql
Copy code
SELECT FirstName, LastName
FROM Employees
Explanation:
This query retrieves employees who are also managers by checking if their EmployeeID exists
in the Managers table.
Multiple Queries
Multiple queries can be executed one after the other within the same session:
sql
Copy code
SQL provides mechanisms to handle exceptions (errors) that occur during the execution of queries.
CONTINUE Handler
A CONTINUE handler is used to skip over an error and continue with the execution of the remaining
statements.
sql
Copy code
BEGIN
BEGIN
END;
END;
Explanation:
If an error occurs during the UPDATE, the handler will catch the exception and continue
executing the next statements.
EXIT Handler
An EXIT handler is used to stop the execution of a procedure when an error occurs.
sql
Copy code
BEGIN
BEGIN
END;
END;
Explanation:
When an error occurs during the UPDATE, the EXIT handler stops further execution.
6. Triggers
A trigger is a stored procedure that is automatically invoked by the database when a specific event
occurs (like INSERT, UPDATE, or DELETE).
sql
Copy code
BEGIN
END IF;
END;
Explanation:
This BEFORE INSERT trigger ensures that an employee's age is 18 or older before allowing the
insertion of the record.
sql
Copy code
BEGIN
END;
Explanation:
This AFTER UPDATE trigger logs the changes made to the Age field into an EmployeeHistory
table whenever an employee's age is updated.
Summary of SQL Constructs
User-Defined Functions: Custom functions that perform operations and return values.
Window Functions: Functions like RANK(), LEAD(), LAG(), and ROW_NUMBER() allow
calculations over a set of rows.
Exception Handling: Handles SQL errors and manages control flow using handlers like
CONTINUE and EXIT.
Triggers: Automatic procedures that run in response to INSERT, UPDATE, or DELETE operation
few advanced SQL interview questions and their answers, along with real-time scenarios where they
could be applied:
Answer:
JOIN: Combines rows from two or more tables based on a related column. It can be used
with different types of joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.
UNION: Combines the result sets of two or more SELECT statements into a single result set.
The columns must be of the same type and in the same order. It removes duplicates by
default (use UNION ALL to retain duplicates).
o Example: Combining customer orders from two different regions into one result set.
Real-time Scenario:
Use JOIN when you need to combine related data from multiple tables.
Use UNION when you need to combine results from different queries but from similar
structured datasets.
2. What is a SELF JOIN? Provide an example.
Answer: A SELF JOIN is a join where a table is joined with itself. It is useful when you need to
compare rows within the same table.
Example: Suppose you have an Employees table with the columns EmployeeID, ManagerID, and
EmployeeName. You can use a SELF JOIN to get a list of employees and their managers.
sql
Copy code
FROM Employees E
Real-time Scenario:
In an organization, to list employees and their managers, you need to join the Employees
table with itself using ManagerID and EmployeeID.
Answer: To find duplicates, you can use the GROUP BY clause along with HAVING COUNT(*) > 1.
Example:
sql
Copy code
FROM Orders
GROUP BY CustomerID
This query will return the CustomerID values where the customer has placed multiple orders.
Real-time Scenario:
You might need to identify customers who have placed duplicate orders or records with the
same email in a customer database.
HAVING: Filters groups after GROUP BY has been applied and works with aggregate
functions.
Example:
sql
Copy code
FROM Orders
GROUP BY CustomerID
In this example:
WHERE filters the rows before the grouping operation (only orders after 2024-01-01).
HAVING filters the result of the grouping operation (only customers with a total order
amount greater than 500).
Real-time Scenario:
WHERE is used to filter raw data, while HAVING is used to filter the aggregated data after
GROUP BY.
5. How would you find the second highest salary in an employee table?
Using SUBQUERY:
sql
Copy code
FROM Employees
sql
Copy code
WITH RankedSalaries AS (
FROM Employees
SELECT Salary
FROM RankedSalaries
WHERE Rank = 2;
Real-time Scenario:
In a payroll system, you may need to find the second-highest salary to identify potential pay
gaps or for analysis of salary structures.
Answer: Window functions allow you to perform calculations across a set of table rows that are
related to the current row. The OVER() clause is used to define the window for the function.
Example:
sql
Copy code
FROM Employees;
Real-time Scenario:
In business intelligence or reporting, window functions are useful to rank, calculate running
totals, or find moving averages.
Answer: A CTE provides a way to create a temporary result set that can be referred to within a
SELECT, INSERT, UPDATE, or DELETE statement. It is defined using the WITH keyword.
Example:
sql
Copy code
WITH EmployeeCTE AS (
FROM Employees
Difference:
A CTE is more readable and can be referenced multiple times in the main query. It also
supports recursion.
A subquery is nested within a query and is evaluated once per query execution.
Real-time Scenario:
You might use a CTE for recursive queries, like finding all subordinates in an organizational
hierarchy.
8. Explain the concept of Indexing in SQL. When should you create an index?
Answer: An index is a database object that improves the speed of data retrieval operations on a
table. It works similarly to an index in a book, helping the database find data quickly.
Create indexes on columns that are frequently used in WHERE, JOIN, or ORDER BY clauses.
Avoid indexing columns that are frequently updated, as this can slow down INSERT, UPDATE,
and DELETE operations.
Example:
sql
Copy code
Real-time Scenario:
Answer:
STRING_AGG() (PostgreSQL, SQL Server, and newer versions of MySQL): Similar function to
GROUP_CONCAT().
Example (MySQL):
sql
Copy code
FROM Orders
GROUP BY CustomerID;
This will return a list of products bought by each customer, concatenated into a single string.
Real-time Scenario:
Useful for reporting where you need to display all products bought by a customer in one line
rather than multiple rows.
Example:
sql
Copy code
FROM Customers;
Real-time Scenario:
In a CRM system, if a customer doesn't have a phone number (NULL), you can replace it with
a default value like 'Not Available' in reports.
These are just a few advanced SQL interview questions that can be encountered in real-time
situations, focusing on practical scenarios to assess knowledge and problem-solving ability.
4o mini
You said:
more
ChatGPT said:
ChatGPT
Here are additional advanced SQL interview questions and answers, including real-time scenarios:
11. What is the difference between INNER JOIN and LEFT JOIN?
Answer:
INNER JOIN: Returns rows that have matching values in both tables. If there is no match,
those rows are excluded.
LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and the matching rows
from the right table. If there is no match, NULL values are returned for columns from the
right table.
Example:
sql
Copy code
-- INNER JOIN
FROM Customers
sql
Copy code
-- LEFT JOIN
FROM Customers
Use INNER JOIN when you only want results with matching records in both tables, such as
retrieving orders that have been placed by customers.
Use LEFT JOIN when you need to retrieve all customers, including those who haven't placed
any orders, and show NULL for missing order data.
Answer:
UNION: Combines the result sets of two or more SELECT queries. It removes duplicates by
default. Use UNION ALL to retain duplicates.
INTERSECT: Returns only the rows that are common to both result sets.
Example:
sql
Copy code
-- UNION
UNION
-- INTERSECT
INTERSECT
Real-time Scenario:
Use UNION when you want to combine results from multiple datasets, such as fetching
unique customers who have placed orders and those in the customer database.
Use INTERSECT when you need to find the common customers who exist in both the Orders
and Customers tables.
Answer: A transaction in SQL is a sequence of operations performed as a single logical unit of work.
Transactions ensure data integrity and consistency.
COMMIT: Finalizes the transaction and saves all changes made during the transaction.
Example:
sql
Copy code
-- Start transaction
BEGIN;
COMMIT;
Real-time Scenario:
In an e-commerce system, you might want to ensure that an order is successfully added to
the Orders table and that inventory is updated. If an error occurs during the process, you
would use ROLLBACK to undo the changes and ensure consistency.
Answer: The EXPLAIN keyword is used to analyze how SQL queries are executed. It provides details
about the query execution plan, such as the indexes used, join types, and the number of rows
scanned.
Example:
sql
Copy code
Real-time Scenario:
Answer: A recursive CTE is a CTE (Common Table Expression) that references itself in order to
perform hierarchical or recursive queries.
sql
Copy code
FROM Employees
UNION ALL
FROM Employees E
Real-time Scenario:
In an employee hierarchy or organization chart, you can use a recursive CTE to retrieve all
subordinates of a specific manager or trace the organizational structure from top to bottom.
Answer: These are window functions used to assign a rank to rows within a partition.
ROW_NUMBER(): Assigns a unique number to each row, without any gaps in the sequence.
RANK(): Assigns a rank to each row, with gaps in the ranking sequence when there are ties.
Example:
sql
Copy code
FROM Employees;
Real-time Scenario:
Use RANK() when you need to rank items and allow gaps for ties (e.g., in a sales
competition).
Use DENSE_RANK() when you want consecutive ranks even for tied values (e.g., in a ranking
list where multiple employees share the same rank).
Answer: Pagination is commonly used to display a subset of rows from a large dataset, typically in
web applications.
sql
Copy code
ORDER BY EmployeeID
This query returns 10 rows starting from row 21 (after skipping the first 20 rows).
Real-time Scenario:
When displaying search results or product listings, pagination is used to limit the number of
results per page.
Answer:
Clustered Index: Defines the physical order of data in the table. A table can have only one
clustered index.
Non-clustered Index: Does not alter the physical order of data but creates a separate
structure that points to the table rows.
Example:
sql
Copy code
Real-time Scenario:
Use a clustered index on the primary key or frequently queried column for fast retrieval.
Use a non-clustered index on columns that are frequently used in search conditions but are
not the primary key.
Indexing: Create indexes on columns that are frequently used in WHERE, JOIN, or ORDER BY
clauses.
Breaking complex queries into smaller parts: Refactor large queries to use temporary tables
or CTEs.
Real-time Scenario:
In a reporting system, large datasets can result in slow performance. Using proper indexing,
query optimization techniques, and analyzing execution plans helps achieve faster query
responses.
Answer:
DELETE: Removes rows one by one and can be rolled back if inside a transaction. It can have
a WHERE clause to delete specific rows.
TRUNCATE: Removes all rows in the table and cannot be rolled back unless inside a
transaction. It is faster than DELETE because it doesn't log individual row deletions.
Example:
sql
Copy code