0% found this document useful (0 votes)
2 views42 pages

SQL

The document provides an overview of SQL, covering types of databases, basic SQL operations, query execution order, and advanced SQL queries. It explains various SQL commands like DDL, DML, DQL, and DCL, as well as the importance of query execution order for performance optimization. Additionally, it discusses joining tables, subqueries, common table expressions, set operations, and window functions, offering scenarios for practical application.

Uploaded by

Abhishek Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
2 views42 pages

SQL

The document provides an overview of SQL, covering types of databases, basic SQL operations, query execution order, and advanced SQL queries. It explains various SQL commands like DDL, DML, DQL, and DCL, as well as the importance of query execution order for performance optimization. Additionally, it discusses joining tables, subqueries, common table expressions, set operations, and window functions, offering scenarios for practical application.

Uploaded by

Abhishek Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 42

SQL BY VENKAT REDDY

- DE Training by Venkat Reddy(NreshIT)


SQL
TYPES OF DATABASES
• Relational vs Non-Relational database
• Relational:
• A relational database is a type of database that organizes data into structured tables (also called
relations), where each table consists of rows and columns. Each row (record) represents a unique
entry, and each column (attribute) holds specific data about that entry.
• Example: Managing customer orders

• Non-Relational :
• A non-relational database, also known as a NoSQL database, is a type of database that stores
and manages data in a flexible, non-tabular format, unlike the structured tables used in relational
databases. Non-relational databases are designed to handle unstructured or semi-structured data,
allowing for a more flexible schema or even schema-less data storage, making them ideal for
applications where data formats vary, change frequently, or don't fit well in a rigid table structure.
• Example: Storing product information
BASIC OPERATIONS IN SQL
BASIC SQL OPERATIONS
• DDL (Data Definition Language)
• It is a type of SQL command used to define data structures and modify data. It creates, alters, and
deletes database objects such as tables, views, indexes, and users.
• Examples of DDL statements include CREATE, ALTER, DROP and TRUNCATE.

• DML (Data Manipulation Language)


• It is a type of SQL command used to manipulate data in a database. It inserts, updates, and deletes data
from a database table.
• Examples of DML statements include INSERT, UPDATE, and DELETE.
• How do you enter the details into the table?
• How can you update the details?
• How to delete the records ?

• Data Query Language (DQL)


• It is used to retrieve data from a database. The main DQL command is SELECT, which allows you to
query or search through tables to find specific information.
• Example: SELECT query
BASIC SQL OPERATIONS
• DCL (Data Control Language)
• DCL includes commands such as GRANT and REVOKE which mainly deal
with the rights, permissions, and other controls of the database system.
• Example:
• GRANT: This command gives users access privileges to the database.
• REVOKE: This command withdraws the user’s access privileges given by using
the GRANT command.
SQL QUERY EXECUTION ORDER
SQL QUERY EXECUTION ORDER
• Why is Order of Execution in SQL Important?
• The order of execution of SQL Query optimizes the query, reduces the
amount of data to be processed, and significantly affects the performance
of the Query processing.
• The order makes the SQL engine process the queries faster, and efficiently
and obtains optimized query results. Understanding this SQL query
execution order helps to debug the code, write efficient queries, and trace
the output of SQL accurately.
SQL ORDER OF EXECUTION

• FROM: First, the table on which the DML operation is performed has to be processed. So,
the FROM clause is evaluated first in an SQL Query.
• WHERE: After the table data on which other operations take place is processed by JOIN
and FROM clause, WHERE clause is evaluated. WHERE clause filters the rows based on
conditions from the table evaluated by the FROM clause. This WHERE clause discards
rows that don’t satisfy the conditions, thus reducing the rows of data that need to be
processed further in other clauses.
• GROUP BY: If the query has a GROUP BY clause, it is executed next. Here, the data is
grouped based on the common value in the column specified in the GROUP BY clause.
This reduces the number of rows further equal to no of distinct values in the GROUP BY
column. This helps to calculate aggregate functions.
SQL ORDER OF EXECUTION
• HAVING: If the query had a GROUP BY clause, the HAVING clause is evaluated immediately
after GROUP BY. HAVING clause is not compulsory for GROUP BY. Similar to WHERE
operations, this clause also filters the table group processed before by the GROUP BY
clause. This HAVING also discards rows that don’t satisfy the conditions, thus reducing the
rows of data that need to be processed further in other clauses
• SELECT: The SELECT is executed next after GROUP BY and HAVING. It computes
expressions (arithmetic, aggregate, etc.) and aliases given in the SELECT clause. The
computation is now performed on the smallest dataset after much filtering and grouping
operations done by previous clauses.
• DISTINCT: The DISTINCT clause is executed after evaluating expressions, and alias
references in the previous step. It filters any duplicate rows and returns only unique rows.
SQL ORDER OF EXECUTION
• ORDER BY: After executing all the above clauses, the data to be
displayed or processed is computed. Now ORDER BY is executed to sort
it based on particular column(s) either in ascending or descending
order. It is left associative, that is it is sorted based on the first
specified column and then by the second, and so on.
• LIMIT/OFFSET: At last, after the order of data to be processed is
evaluated, LIMIT and OFFSET clauses are evaluated to display only the
rows that fall within the LIMIT. So, it is generally not recommended to
LIMIT only certain rows from many rows evaluated before, since It is not
efficient and waste of computation.
EXAMPLE: SQL QUERY EXAMPLE FOR ORDER OF EXECUTION

• Objective: Retrieve a list of doctors with the total number of completed


appointments in 2024, only including doctors who had at least 5
completed appointments in this period. The results should be ordered
by the total number of appointments in descending order.
EXPLANATION FOR ABOVE QUERY
In SQL, the execution order of a query doesn’t necessarily follow the order of clauses in the written syntax.
Here's the actual execution order of the clauses in this query:
FROM: The query starts by identifying the table from which to retrieve data (Appointments)
WHERE: The WHERE clause filters the rows based on the condition specified. Only rows where status =
'Completed' and appointment_date is in the year 2024 are selected.
GROUP BY: After filtering, the GROUP BY clause groups the rows by doctor_id, so we can perform
aggregate functions on each group.
HAVING: The HAVING clause applies a filter to the groups created by GROUP BY. In this case, it keeps only
those groups (i.e., doctors) with a COUNT of appointment_id that is 5 or more.
SELECT: The SELECT clause is processed next. It retrieves doctor_id and the calculated
total_completed_appointments (the result of COUNT(appointment_id)).
ORDER BY: Finally, the results are sorted by total_completed_appointments in descending order.
ADVANCED SQL QUERIES

• Complex select statements:


• Scenario: Retrieve the patient name, doctor name, appointment date, and
department for all scheduled appointments, sorted by department and
appointment date. If the appointment has a note, include it; otherwise,
show "No Notes Available.”
• Here we use from,joins, where, select, order by
SELECT DISTINCT AND USING ALIAS (AS)

• Scenario :Find all unique appointment statuses recorded in the hospital.


• Scenario2: Show a simplified list of appointments with the patient's
name, the doctor they are seeing, and the date of the appointment.
Use aliases to give descriptive names to columns.
Working with NULL Values

• Scenario: Find all appointments where the notes field is not provided
(i.e., NULL).
AGGREGATE FUNCTIONS AND GROUPING

• These are aggregate functions used to perform calculations on a set of rows and return a
single value - COUNT, SUM, AVG, MIN, MAX
• COUNT: Counts the number of rows or non-NULL values.
• SUM: Calculates the total of numeric values.
• AVG: Returns the average of numeric values.
• MIN: Finds the smallest value.
• MAX: Finds the largest value.

• GROUP BY clause
• GROUP BY organizes rows into groups based on a column’s value.
• Used with aggregate functions to perform calculations for each group separately

• HAVING clause
• HAVING filters groups created by GROUP BY, based on aggregate values.
• Similar to WHERE, but works on grouped (aggregated) data.
BUSINESS SCENARIOS

• SSMS Examples
WHEN TO USE THESE CLAUSES:

1.COUNT, SUM, AVG, MIN, MAX:


1. To get summarized metrics, like total revenue, average consultation time, or
the range of fees.

2.GROUP BY Clause:
1. To break data into categories or groups (e.g., by doctor, department, or patient
demographics).

3.HAVING Clause:
1. To filter results of grouped data based on aggregate values, such as identifying
departments with high revenue or doctors with many appointments.
JOINING THE TABLES
• INNER JOIN
• Definition: An INNER JOIN retrieves rows that have matching values in both tables based on the
specified condition.
• Scenario : The hospital wants a list of patients who have scheduled appointments, including their
names, appointment dates, and fees.

• LEFT JOIN and RIGHT JOIN


• A LEFT JOIN retrieves all rows from the left table and the matching rows from the right table. Rows from
the left table with no match will show NULL for right table columns.
• A RIGHT JOIN is the opposite: all rows from the right table and the matching rows from the left table.
• Left : The hospital wants to list all patients, including those who haven’t booked any appointments.
• Right: The management wants to list all appointments, including those that aren’t associated with any
patient.
JOINS

• FULL OUTER JOIN: A FULL OUTER JOIN retrieves all rows from both tables, matching rows
where possible. Unmatched rows from either table will have NULL values for missing columns.
• Scenario: The hospital wants a complete list of all patients and all appointments, showing
matches where possible.

• SELF JOIN: A SELF-JOIN is when a table is joined to itself. Often used to compare rows within the
same table.
• Scenario: The hospital wants to know which staff members report to whom.

• Cross Join: A CROSS JOIN produces a Cartesian product of two tables, pairing every row from the
first table with every row from the second.
• Scenario : The hospital wants to see all possible combinations of doctors and departments to
explore possible rotations.
JOINS SUMMARY

JOIN Type Scenario Purpose

INNER JOIN Match rows in both tables Only include rows with matches.

Include all rows from the left Include unmatched rows from the
LEFT JOIN
table left table.

Include all rows from the right Include unmatched rows from the
RIGHT JOIN
table right table.

Combine matched and


FULL OUTER JOIN Include all rows from both tables unmatched rows from both
tables.

Compare rows within the same Explore hierarchical or


SELF-JOIN
table relationship data.

Generate all combinations


CROSS JOIN Test scenarios or explore options.
between two tables
SUBQUERIES AND NESTED SUBQUERIES
• Basic Subquery: A subquery is a query nested inside another query. Subqueries can be used to filter results, perform calculations, or define criteria
for the main query.

• Single Row Subquery


• A single row subquery returns zero or one row to the outer SQL statement. You can place a subquery in a WHERE clause, a HAVING clause,
or a FROM clause of a SELECT statement.

• Scenario : Find the department of the doctor with the highest experience.

• Multiple-Row Subqueries
• Multiple row subquery returns one or more rows to the outer SQL statement. You may use the IN, ANY, or ALL operator
in outer query to handle a subquery that returns multiple rows.
• Scenario: List the names of patients who have scheduled appointments with doctors in the "Oncology" department.
SUBQUERIES AND NESTED SUBQUERIES

• Correlated Subqueries
• In a correlated subquery, the inner query refers to columns from the outer
query. This subquery runs once for each row of the outer query.
• Scenario : Find doctors who have handled more than one appointment.

• Using EXISTS and NOT EXISTS


• EXISTS: Checks if a subquery returns any rows.
• NOT EXISTS: Ensures that a subquery returns no rows.
• Scenario: List departments with at least one doctor assigned.
COMMON TABLE EXPRESSIONS (CTES)
• A Common Table Expression (CTE) is a temporary result set that
simplifies queries and improves readability. It can also be recursive for
hierarchical data.
• It is used within the execution scope of a SELECT, INSERT, UPDATE, or
DELETE statement. CTEs are designed to make queries easier to read
and maintain by allowing complex queries to be broken into modular
components.
• Syntax and Scenario: Find the total appointment fees collected by each
department.
SET OPERATIONS

• 1. UNION and UNION ALL


• UNION combines the results of two or more queries and removes duplicates.
• UNION ALL combines the results but keeps duplicates

• Find all unique names of hospital staff and doctors


SET OPERATIONS

• 2. INTERSECT : INTERSECT retrieves only the rows that are common between two queries.

• Scenario: Identify names that are common between the Staff and Doctors tables (e.g., a staff
member who is also a doctor).
SET OPERATIONS

• 3. EXCEPT: EXCEPT retrieves rows from the first query that are not present in the second query.

• Find names of doctors who are not listed as hospital staff


CASE WHEN STATEMENT

• CASE WHEN Statement: The CASE WHEN statement is used to apply


conditional logic in SQL queries, allowing the transformation or
classification of data based on specific criteria.
• Syntax:
CASE WHEN STATEMENT

• Scenario : Classify patients as "Minor", "Adult", or "Senior" based on


their age.
• Scenario2 : Determine whether appointments are "On Time", "Late", or
"Missed" based on appointment status.
CASE WHEN WITH GROUP BY

• Combining CASE WHEN with GROUP BY allows aggregation based on


classifications.
• Syntax:
CASE WHEN WITH GROUP BY STATEMENT

• Scenario1 : Count the number of patients in each age group.


• Scenario2 : Calculate the total revenue collected for "Completed" and
"Cancelled" appointments.
WINDOW FUNCTIONS IN SQL
• Window functions allow you to perform calculations across a "window" of rows related to the current row
without collapsing the result set. They are particularly useful for ranking, running totals, comparisons,
and advanced analytics.
• Key Elements of Window Functions
• OVER() Clause: Defines the scope or "window" of rows for the function.
• PARTITION BY Clause: Divides the dataset into partitions (groups) for calculations.
• ORDER BY Clause: Specifies the order of rows within each partition or window.
• Types of Window Functions:
• Ranking Functions: Assign a rank or position to rows (e.g., ROW_NUMBER, RANK, DENSE_RANK).
• Aggregate Functions as Window Functions: Perform calculations such as SUM, AVG, or COUNT but maintain
row-level data.
• Analytic Functions: Provide advanced insights, such as LEAD, LAG, FIRST_VALUE, LAST_VALUE, or NTILE.
• Frame Clause: Specifies subsets of rows for each calculation using ROWS BETWEEN or RANGE BETWEEN.
WINDOW FUNCTIONS IN SQL
• Basic Syntax:
• SELECT column1,window_function() OVER ([PARTITION BY column2] [ORDER
BY column3]) AS alias_name FROM table_name;
WINDOW FUNCTIONS IN SQL
• Ranking Window functions:
• Row_Number(): The ROW_NUMBER function assigns a unique
sequential number to each row within a partition (group of rows). Even
if multiple rows have the same values in the column(s) being ordered,
ROW_NUMBER will not allow ties and will assign a unique number to
each row.
• Usecase: When you need a strict sequence for rows, regardless of whether
they are identical in ranking criteria.
• Scenario: Rank appointments for each doctor by fee in descending order,
with no ties allowed.
AGGREGATED WINDOW FUNCTIONS

• SUM as a Window Function


• Definition:The SUM window function calculates a running total or group
total for each row in the dataset, maintaining the original row structure.

• You want to know the total fees collected by each doctor for their
appointments while keeping row-level data.
AGGREGATED WINDOW FUNCTIONS

• AVG
• The AVG window function calculates the average of a column across rows in
a group while keeping the individual rows intact.
• Scenario: You want to find the average fee collected by each doctor for
their appointments.

• COUNT
• The COUNT window function calculates the number of rows in a group or
partition while maintaining row-level data.
• Scenario: You want to know how many appointments each doctor has.
WINDOW FUNCTIONS: ANALYTIC FUNCTIONS
• Analytic functions provide advanced insights by analyzing and
comparing data within partitions or rows. These include LEAD, LAG,
FIRST_VALUE, LAST_VALUE, and NTILE. They help in scenarios like trend
analysis, identifying first or last records, or dividing data into equal
parts.
• LEAD Function: The LEAD function retrieves the value of a column from
the next row in the same partition. It's used to compare current rows
with future rows.
• Scenario: You want to know the fee of the next appointment for each
doctor
WINDOW FUNCTIONS: ANALYTIC FUNCTIONS
• LAG Function : The LAG function retrieves the value of a column from
the previous row in the same partition. It is often used to compare
current rows with past rows.
• Scenario: You want to know the fee of the previous appointment for
each doctor.
WINDOW FUNCTIONS: ANALYTIC FUNCTIONS
• FIRST_VALUE Function
• The FIRST_VALUE function retrieves the first value of a column in the
window or partition.
• Scenario : You want to know the fee of the first appointment for each
doctor.

• NTILE Function
• The NTILE function divides rows in a partition into a specified number of
groups and assigns a rank (bucket number) to each row.
• Scenario: You want to divide appointments for each doctor into two equal
groups based on their fees.
WINDOW FUNCTIONS: ANALYTIC FUNCTIONS
• The frame clause specifies a subset of rows relative to the current row within a
window. It is used in window functions to define which rows are included in the
computation. The two primary clauses are:
• RANGE BETWEEN: Operates on the logical value of rows (e.g., based on ORDER BY values).
• ROWS BETWEEN: Operates on the physical row positions within the result set.

• RANGE BETWEEN
• Defines a range of rows based on values in the ORDER BY clause. It is used for scenarios
like cumulative sums or averages over a specific value range.
• Scenario: You want to calculate the cumulative sum of fees for each doctor up to the
current appointment
BEST EXAMPLE FOR WINDOW FUNCTIONS

• https://www.sqlshack.com/use-window-functions-sql-server/
• https://www.sqlshack.com/overview-of-mysql-window-functions/
• https://datalemur.com/sql-tutorial/sql-union-intercept-except

You might also like