SQL
SQL
SQL
Introduction
Types of data
Unstructured Data (NoSQL - Not only SQL): Eg. Audios, images, Cassandra, Hbase (Hives), Hadoops, Maria DB,
Mongo DB.
Semi-structured Data: Combination of structured and unstructured data. Eg. Wikipedia
Types of Databases
Introduction to SQL
It is the language that is used to communicate with data that is stored in relational database
Everything we do online generates data, and this data needs to be stored in order to analyze it in future
Every data has a type and it is called as datatype. There are different types of datatypes in SQL that can be
utilized in order to store various forms of data.
Create
Read
Update
Delete
Structured data
When the data is stored in rows and columns and stored in tables, it’s called structured data. And to talk to
structured data, we have SQL
When we put these type of multiple tables together, it’s called a database.
Collection of tables is called as database, and to manage we need a system, that is called as database
management system.
Rows+column-->Tables-->Multiple tables-->Database
Role of a database
Server has no capability to store the data, but it has some storage to store the codes or programs
SQL 1
Statements
USE database_name;
Dropping a table
UPDATE statement
2. You can update one or more columns for all rows or specific rows that match a certain condition.
3. If no condition is specified with WHERE clause then it will update for the whole table that’s why it’s important
to mention the WHERE clause and its condition
Syntax
UPDATE table_name
SET column1_name = value1, column2_name = value2,...
WHERE condition;
Eg
SQL 2
UPDATE employee_demographics
SET first_name = "Leslie", last_name = "Knope"
WHERE employee_id = 1
UPDATE employee_salary
SET salary = "40000"
WHERE employee_id IN (1,2);
DELETE statement
1. The DELETE statement in SQL is used to remove rows from a table. It can be used with or without a WHERE
clause.
2. If a WHERE clause is not specified, all rows in the table will be deleted, which can be dangerous and is
generally not recommended unless you intend to clear the entire table.
Syntax
Eg
INDEX
An index in SQL is a data structure that improves the speed of data retrieval operations. Think of it like a table
of contents in a book — it helps quickly locate the data you're looking for, without having to scan the entire
table.
Syntax
Guidelines of Index
Automatically creates the indexes for PRIMARY KEY and UNIQUE columns
Index columns that are used for joins to improve join performance
VIEW command
SQL 3
A view is a virtual table in SQL that represents the result of a stored query. It does not store data physically;
instead, it stores the SQL query used to generate the data when needed. Views can be used to simplify
complex queries, restrict access to certain columns or rows, and abstract data complexity.
2. Security: Views allow you to restrict access to specific data. For example, you can create a view that only
exposes certain columns, hiding sensitive data.
3. Data Abstraction: You can abstract complex logic by storing it in a view, so users don’t have to write
complicated SQL each time.
4. Reusability: Once a view is created, it can be reused in different parts of your application or by different
users.
OR
SELECT*
FROM view_name
SELECT*
FROM new_view_name;
ALTER command
The ALTER command in SQL is used to modify the structure of an existing table. It allows you to add, modify, or
drop columns, constraints, and other table attributes.
SQL 4
ALTER TABLE table_name
ADD COLUMN column_name column_definition;
Example:
Modify a Column
Change the definition of an existing column (e.g., data type, size).
Example:
Rename a column
Example:
Drop a column
Remove an existing column from the table.
Example:
Example:
Add a constraint
Add a constraint like a primary key, unique constraint, or foreign key.
Primary Key:
SQL 5
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column_name);
Example:
Foreign Key:
Example:
Drop a constraint
Example:
1. A ROLLBACK statement in SQL is used to undo transactions that have not yet been saved to the database. It
reverts the database back to the state it was in before the transaction began.
2. COMMIT: Saves the changes made during the transaction to the database. Once this is pressed, the data
can’t be rolled back i.e. the change is permanent
Transactions
The purpose and meaning of transactions in MySQL (or any database) revolve around ensuring data
consistency, integrity, and reliability in cases where multiple operations need to be treated as a single unit.
Let's break it down:
What is a Transaction?
A transaction is a sequence of one or more SQL operations that are executed as a single unit. It follows the
ACID properties to ensure the data remains consistent and reliable, even in the event of errors, crashes, or
other unexpected issues.
All or Nothing: Either all operations in a transaction are successfully completed, or none of them are
applied. For example, if you’re transferring money from one bank account to another, both the debit and
credit operations must succeed together.
2. Consistency:
SQL 6
Ensures the database moves from one valid state to another. If the transaction fails, the database is
"rolled back" to its state before the transaction started.
3. Isolation:
Transactions operate independently of one another. If multiple users are working on the same database,
each transaction remains isolated until it’s completed.
4. Durability:
Once a transaction is committed, the changes are permanent, even in case of system failure.
Without Transactions:
Imagine a banking application that transfers ₹500 from Account A to Account B using two operations:
If the first operation succeeds but the second one fails (e.g., due to a network issue), your database will now
have inconsistent data:
With Transactions:
The same operations are wrapped in a transaction:
Syntax
);
String
S. No Datatypes Size
1 CHAR(size) 0 to 255
2 VARCHAR(size) 0 to 65535
3 BINARY(size) 0 to 255
4 VARBINARY(size) 0 to 65535
SQL 7
6 TEXT(size) 65,535 bytes
Numeric
1 BIT(size) 1 to 64
-9223372036854775808 to
7 BIGINT(size)
9223372036854775807
8 BOOL 0 or 1
9 BOOLEAN 0 or 1
10 FLOAT(p)
‘1000-01-01’ to ‘9999-12-
1 DATE Year-month-date
31’
4 TIME(fsp) hh:mm:ss
Eg
Syntax
SQL 8
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2)
Eg
Syntax
2. UNIQUE - This keyword can be used where there will always be unique identity such as phone number and ID
3. DEFAULT - If you don’t fill it, it should get filled by default like city name.
4. CHECK - Can be used to put condition. The block can only be filled when the conditions would be fulfilled
5. PRIMARY KEY - Always has unique data and can’t have a null value. A table can have only one primary key
constraint such as id can be set as primary key because it’s always unique and doesn’t have null value but
name can’t be because names can be same.
6. FOREIGN KEY - Key used to link two tables together. Foreign key in one table is used to point primary key in
another table
Syntax with eg
SQL 9
PRIMARY KEY (id)
);
Syntax
Eg.
SQL 10
SELECT*
FROM employee_demographics
WHERE gender = "Female";
Operator Description
= Equal
AND operator
If we have to imply multiple conditions then we can utilize these operators .
OR operator
IN operator
IN operator - Tells exactly those values that are entered in this operator
SQL 11
WHERE age IN (36,44)
NOT operator
NOT IN operator
NOT IN operator - Gives all the value that are not mentioned
LIKE operator
LIKE operator can be used to perform pattern matching in SQL queries.
SQL 12
_ underscore sign - Represents a single character
Pattern Description
Syntax
SELECT *
FROM table_name
WHERE column_name LIKE pattern;
SELECT *
FROM table_name
WHERE column_name NOT LIKE pattern;
April Ludgate
Ann Perkins
Andy Dwyer
This will be the output, it will be either the names starting with A or that has pr within it or it can be both as
in case of April
NULL Operator
In MySQL, the IS NULL and IS NOT NULL operators are used to check whether a column's value is NULL or not.
SQL 13
SELECT column1, column2...
FROM table_name
WHERE column IS NOT NULL;
SELECT age
FROM employee_demographics
WHERE age IS NOT NULL;
Regular Expression
Regular expressions can be used for pattern matching in SQL queries. The function REGEXP (or RLIKE , which is a
synonym) is used to match a string against a regular expression pattern.
Syntax
SELECT column_name
FROM table_name
WHERE column_name REGEXP 'pattern';
Pattern with an
Sign Description
eg
Any character listed between the square brackets. That is the word should have either r
or m or s as given in the eg. 1. Also it can be used as ‘[rm]s’. It will search as rs and ms.
[…] ‘[rms]’
2. If it’s sth like this ‘^[rs]’, it will search for words that either start with r or with s. 3. If it’s
sth like this ‘[rs]$’. it will search for words that either ends with r or with s
[a-z] ‘[a-h]e’ Match with in the range like ae, be, ce, de, …. he
Matches any of the patterns p1, p2, and p3. This can be used with ^, $ sign as well for
p1|p2|p3 ‘tom|dick|harry’
finding the first and last name specifically
ORDER BY
It can be used to keep things in order either in ascending order or descending order. Even multiple things can be
kept in order. But the priority will be the first object
Syntax
Eg
SELECT *
FROM employee_demographics
ORDER BY first_name DESC ;
SELECT *
FROM employee_demographics
WHERE age > 20
ORDER BY first_name DESC ;
DISTINCT
SQL 14
The DISTINCT keyword in MySQL is used to remove duplicate rows from the result set.
Syntax
Eg
SELECT age
FROM employee_demographics
ORDER BY age
LIMIT 3
SELECT*
FROM employee_demographics
WHERE gender = "Female"
ORDER BY age
LIMIT 4
SELECT*
FROM employee_demographics
ORDER BY age
LIMIT 3, 3
Aggregate functions
1. The COUNT function returns the number of rows that match a specified condition.
Syntax
Eg
SQL 15
SELECT COUNT(DISTINCT gender) AS Gender
FROM employee_demographics
JOINS
Inner Join
An inner join in SQL is used to combine rows from two or more tables based on a related column between them.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
SELECT*
FROM personal AS p
INNER JOIN city AS c
ON p.city = c.cid;
Outer Join
Left Join
A left outer join returns all rows from the left table ( table1 ), and the matched rows from the right table
( table2 ). If there is no match, NULL values are returned for the columns from table2 .
Syntax
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Eg
SQL 16
6 Donna Meagle 6 Donna Meagle
Right Join
A RIGHT JOIN , is a type of join operation in SQL that returns all rows from the right-hand table ( table2 ), and
the matched rows from the left-hand table ( table1 ). If there is no match, NULL values are returned for the
columns from table1 .
Syntax
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Eg
SELECT column_names
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;
Cross Join
1. Join where each row from the first table is combined with every row from the second table. Essentially, it
produces a Cartesian product of the two tables involved in the join.
SQL 17
4. It’s mostly useless only
SELECT columns
FROM table1
CROSS JOIN table2;
SQL 18
Id Name Age City Course
Natural Join
1. The NATURAL JOIN in SQL automatically joins two tables based on all columns with the same name and
compatible data types in both tables. It eliminates the need to explicitly specify the ON clause, as the join
condition is implicitly based on the shared column names. If there is any problem with the same type of
columns, it will automatically become cross join.
2. Just like CROSS JOIN this also doesn’t need any JOIN condition
SELECT column_names
FROM table1
NATURAL JOIN table2;
Self Join
A self join is a type of SQL join in which a table is joined with itself. It is useful when you need to compare
rows within the same table.
Often used for hierarchical or comparative data, such as finding relationships (e.g., employees and their
managers), duplicates, or specific patterns.
GROUP BY
2. This clause is used in conjunction with the SELECT statements and Aggregate functions to group rows together
by common column values.
3. If you want to use “WHERE” clause then you have to use it before GROUP BY clause
Syntax
SELECT columns
FROM table_name
WHERE condition (optional)
GROUP BY column_name;
SELECT columns
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition (optional)
GROUP BY column_name;
SQL 19
Eg
Limitations
In SQL, when you use GROUP BY , you must either:
Use an aggregate function (such as MAX , SUM , etc.) for the column.
This will throw an error since emp_NAME is not aggreagated or used in GROUP BY
HAVING clause
If you’ve to put some condition after GROUP BY has happened then you can utilize HAVING clause
SELECT columns
FROM table_name
WHERE condition (optional)
GROUP BY column_name
HAVING condition
ORDER BY condition (optional)
FROM personal AS p
INNER JOIN city AS c
ON p.city = c.cid
WHERE p.age>20
GROUP BY city
HAVING COUNT(p.city)>=1
ORDER BY Total;
In SQL, UNION and UNION ALL are used to combine the results of two or more SELECT queries. They have some key
differences and use cases:
SQL 20
UNION
Combines the results of two or more SELECT statements.
Removes duplicate rows from the result set. It only removes duplicates when all the other parameters are same
as well. Suppose we just took names and there are 2 similar names, it would just show 1 in the table. But let’s if
we select all the parameters then it would show us two names since they might have different id, number, city.
UNION ALL
Syntax
Rules -
1. Each SELECT statement within UNION must have the same number of columns.
2. The columns must also have similar data types like INT, VARCHAR
Eg
SELECT name,age
FROM students
WHERE gender = "M"
UNION
SELECT name, age
FROM lecturers
WHERE gender = "M";
CASE Clause
Syntax
SQL 21
SELECT column1, column2,
CASE
WHEN condition1 THEN RESULT1
WHEN condition2 THEN RESULT2
WHEN condition3 THEN RESULT3
ELSE result alias_name
END AS alias_name
FROM table_name;
Eg
UPDATE student_table
SET Percentage = (CASE Id
WHEN 3 THEN 91
WHEN 2 THEN 15
END)
WHERE Id in (3,2);
Syntax
Eg
SQL 22
FROM student_table;
Functions in SQL
Integer Function
1. MOD()
Example:
2. FLOOR()
Syntax: FLOOR(number)
Example:
3. CEIL() or CEILING()
Example:
4. ROUND()
Example:
5. TRUNCATE()
Example:
SQL 23
SELECT TRUNCATE(5.678, 1); -- Output: 5.6
SELECT TRUNCATE(5.678, 0); -- Output: 5
6. ABS()
Syntax: ABS(number)
Example:
7. SIGN()
Syntax: SIGN(number)
Example:
8. DIV OR /
Example:
9. POW() or POWER()
Example:
Syntax:
Example:
SQL 24
SELECT GREATEST(10, 20, 5); -- Output: 20
SELECT LEAST(10, 20, 5); -- Output: 5
String Functions
String Functions - I
3. CHAR_LENGHT() - Used for calculating the length of the string. It includes space as well.
4. LENGTH() - Gives the value in bytes for how much space is occupied.
8. TRIM() - Can trim the space from both left and right side
9. POSITION() - Can tell the position of a particular word or character in the string using number
10. INSTR() - It also tells the position but we don’t have to use IN
SQL 25
11. LOCATE() - It also tells the position but the syntax is different.
Output will be 3
12. SUBSTRING() - Gives the value of string from the number provided
Name
o Baba
Name
Bab
13. SUBSTRING_INDEX()
It gives us all the value before the 2nd parameter and 3rd parameter decides which 2nd parameter to
use
Name
www.yahoobaba
String Functions - II
yahoo
aba
If we want to make a certain string up to a certain length towards right side then RPAD can be utilized.
Yahoo baba combined has 10 characters, so to make it 20, we will add 10 dashes.
Yahoo baba----------
SQL 26
17. LPAD(Parameter1, Parameter2, Parameter3)
----------Yahoo baba
18. REVERSE()
001
19. REPLACE()
Twitter Baba
20. CAST()
This is utilized to convert into int format. Replace function returns in string format
CAST(__ AS SIGNED)
21. STRCMP()
Used for matching two strings
22. FIELD()
The FIELD() function in SQL is used to find the position of a value within a list of values. It is most
commonly available in MySQL. If the value does not exist in the list, the function returns 0 .
23. FIND_IN_SET()
Same as field but we don’t have to leave space in between inverted commas and also don’t leave any
space in between commas (grammar works differently here)
SQL 27
5961686F6F2042616261
26. REPEAT
The REPEAT() function in SQL is used to repeat a given string a specific number of times. It is a simple
but powerful function, especially when you need to generate patterns or create formatted output.
REPEAT(string, count)
Date Functions
Initial functions
SELECT CURRENT_TIME();
SELECT CURRENT_DATE()
SELECT CURRENT_TIMESTAMP()
For extracting anything - day, month, year and second, minute, hour
Component includes
Later functions
SQL 28
SELECT DATEDIFF('2024-02-21', '2024-02-10')
Eg
Eg.
Last functions
💡 The format will always be year-month-date because SQL understands that only
SQL 29
Null Functions
IFNULL()
Introduction
Replaces ‘NULL’ with a specified value. If the value is not null, it will return whatever value is specified
like in this case value otherwise in case of null values, it will return replacement_value .
Ex
SELECT
first_name,
IFNULL(middle_name, 'No middle name') AS middle_name,
last_name
FROM employees;
Explanation:
IFNULL(middle_name, 'No middle name') : If middle_name is NULL , it will return 'No middle name' instead.
Otherwise, it will return the value of middle_name .
first_name , last_name : These columns are selected as-is without any modification.
Example Output:
first_name middle_name last_name
COALESCE()
SQL 30
COALESCE() is a standard SQL function used to return the first non- NULL value from a list of expressions. If all
the expressions are NULL , it returns NULL .
Explanation:
expression1, expression2, ..., expressionN : These are the values or expressions you want to evaluate.
COALESCE() returns the first non- NULL expression in the list. If all expressions are NULL , it returns NULL .
Key Points:
It can take two or more arguments.
NULLIF()
Syntax
If the value of column_name matches with expression then the value changes to null value
NULLIF(column_name, expression)
Ex
id name salary
1 John 5000
2 Alice 5000
3 Bob NULL
4 Charlie 3000
Window Function
In SQL, a window function is a type of function that performs a calculation across a set of table rows that are
related to the current row. Unlike aggregate functions that return a single result for a group of rows, window
functions return a value for each row in the result set. This allows for more complex and nuanced queries.
MAX() function
Using Partition by
SQL 31
COUNT() function
COUNT(*) —>Counts all rows in the table, regardless whether it’s null or not and their datatype
Needs parameters
lag()
Syntax
SELECT e.*,
lag(SALARY,2,0) over(partition by DEPT_NAME ORDER BY emp_ID) AS lag_salary
FROM employee e;
lead()
SELECT e.*,
lag(SALARY) over(partition by DEPT_NAME ORDER BY emp_ID) AS prev_emp_salary,
lead(SALARY) over(partition by DEPT_NAME ORDER BY emp_ID) AS next_emp_salary
FROM employee e;
Fetch a query to display if the salary of an employee is higher, lower or equal to the previous employee
SELECT e.*,
lead(salary) over(partition by DEPT_NAME ORDER BY emp_ID) as next_emp_salary,
CASE
WHEN e.SALARY>lead(salary) over(partition by DEPT_NAME ORDER BY emp_ID) THEN
WHEN e.SALARY<lead(salary) over(partition by DEPT_NAME ORDER BY emp_ID) THEN
WHEN e.SALARY=lead(salary) over(partition by DEPT_NAME ORDER BY emp_ID) THEN
END sal_comparison
FROM employee e;
first_value()
The FIRST_VALUE function in SQL is an analytic function that returns the first value in a specified ordered set
of values within a partition. It’s commonly used to pull the "first" value based on a defined order for each
partition in a result set. This function is particularly helpful when you need to retrieve the first occurrence of
a value within a group of rows while still keeping all rows in the output.
Syntax
FIRST_VALUE(column_name) OVER (
PARTITION BY partition_column
ORDER BY order_column
SQL 32
[ROWS/RANGE frame_clause]
)
SELECT *,
first_value(product_name) over(partition by product_category ORDER BY price DESC) as
FROM product p;
last_value()
The LAST_VALUE function in SQL is an analytic function that retrieves the last value in a specified ordered set
of values within a partition. It’s similar to FIRST_VALUE , but instead of getting the first item in the ordered set, it
gets the last item.
Syntax
SELECT*,
FIRST_VALUE(product_name) over(partition by product_category ORDER BY price DESC) AS
LAST_VALUE(product_name) over(partition by product_category ORDER BY price DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as most_cheap_pdt
FROM product;
SELECT*,
FIRST_VALUE(product_name) over w AS most_exp_produt,
LAST_VALUE(product_name) over w as most_cheap_pdt
FROM product
window w AS (partition by product_category ORDER BY price DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);
nth_value()
The NTH_VALUE function in SQL is used to retrieve the Nth value in an ordered set of values within a window
or partition. Unlike FIRST_VALUE or LAST_VALUE , which only return the first or last value, NTH_VALUE lets you
specify which position (like 2nd, 3rd, etc.) you want to return. It takes two argument, column name and N
(The position of the value you want to retrieve)
Q. Write a query to display the second most expensive product under each category?
NTH_VALUE(column_name, N) OVER (
PARTITION BY partition_column
ORDER BY order_column
[ROWS/RANGE frame_clause]
)
SQL 33
SELECT*,
nth_value(product_name, 2) over w as second_most_exp_pdt
FROM product
window w AS (partition by product_category ORDER BY price DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);
ntile()
The NTILE function in SQL is used to divide rows into a specified number of buckets or groups and assign
each row a bucket number. It’s helpful for distributing rows evenly or creating percentiles, quartiles, or other
statistical groupings.
Write a query to segregate all the expensive phones, mid range phones and cheaper phones
SELECT product_name,
CASE
WHEN x.buckets = 1 THEN "Expensive Phone"
WHEN x.buckets = 2 THEN "Mid Range Phone"
WHEN x.buckets = 3 THEN "Cheap Phone"
END phone_category
FROM (SELECT*,
ntile(3) over(ORDER BY price desc) AS buckets
FROM product
WHERE product_category = "Phone") AS x
row_number()
SELECT e.*,
row_number() over() AS rn
FROM parks_and_recreation.employee_salary AS e
SELECT e.*,
row_number() over(partition by dept_name) AS rn
FROM employee e;
Now, I want the the names of new joiners assuming new joiners will have greater employee_id than previous
employees.
rank()
This will provide the rank like 1, 2, 3. If the salary is same, same rank will be provided
SQL 34
SELECT e.*,
rank() over(partition by DEPT_NAME ORDER BY SALARY DESC) AS rnk
FROM employee e
This can give me the values of people that have lesser rank than 4
SELECT* FROM
(SELECT e.*,
rank() over(partition by DEPT_NAME ORDER BY SALARY DESC) AS rnk
FROM employee e) AS x
WHERE x.rnk<4
dense_rank()
Only difference is the numbering as you can see in the highlighted values
SELECT e.*,
rank() over(partition by DEPT_NAME ORDER BY SALARY DESC) AS rnk,
dense_rank() over(partition by DEPT_NAME ORDER BY SALARY DESC) AS den_rnk
FROM employee e
rnk den_rnk
SELECT e.*,
rank() over(partition by DEPT_NAME ORDER BY SALARY DESC) AS rnk,
dense_rank() over(partition by DEPT_NAME ORDER BY SALARY DESC) AS den_rnk,
row_number() over(partition by DEPT_NAME ORDER BY SALARY DESC) AS rn
FROM employee e
rnk den_rnk rn
SQL 35
106 Rajesh Finance 5000 4 3 4
Are useless without ORDER BY because their primary function depends on an ordered
evaluation.
Always pair them with an ORDER BY clause in the OVER() window to ensure meaningful and
consistent results.
cume_dist()
The CUME_DIST (cumulative distribution) function in SQL is used to calculate the cumulative distribution of a
value within a partition, which means it shows how many rows have a value less than or equal to the
current row's value. It outputs a cumulative percentile rank between 0 and 1 .
CUME_DIST() OVER (
PARTITION BY partition_column
ORDER BY order_column
)
Write a query to fetch all products which are constituting the first 30% of the data in products table based
on price
percent_rank()
The PERCENT_RANK function in SQL is used to calculate the relative rank of each row as a percentage between
0 and 1 , within a partition. Unlike CUME_DIST , which gives you the cumulative distribution up to each value,
PERCENT_RANK calculates how each row ranks relative to the highest and lowest values in the partition.
PERCENT_RANK() OVER (
PARTITION BY partition_column
ORDER BY order_column
)
Frame clause
SQL 36
Let's look at examples for all the possible frame clause types in SQL, using a sales table:
month sales
Jan 100
Feb 200
Mar 150
Apr 300
We’ll use a window function SUM(sales) to calculate different types of sums based on the frame clauses.
Query:
SELECT
month,
sales,
SUM(sales) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT RO
W) AS running_total
FROM sales;
Result:
month sales running_total
Query:
SELECT
month,
sales,
SUM(sales) OVER (ORDER BY month ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rol
ling_sum
FROM sales;
Result:
month sales rolling_sum
For Feb, includes both Jan and Feb ( 100 + 200 = 300 ).
For Mar, includes both Feb and Mar ( 200 + 150 = 350 ).
SQL 37
For Apr, includes both Mar and Apr ( 150 + 300 = 450 ).
Query:
SELECT
month,
sales,
SUM(sales) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWIN
G) AS future_sum
FROM sales;
Result:
month sales future_sum
Query:
SELECT
month,
sales,
SUM(sales) OVER (ORDER BY month ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWIN
G) AS future_total
FROM sales;
Result:
month sales future_total
For Jan, includes Jan + Feb + Mar + Apr ( 100 + 200 + 150 + 300 = 850 ).
For Feb, includes Feb + Mar + Apr ( 200 + 150 + 300 = 650 ).
SQL 38
5. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
Meaning: This is similar to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW , but it considers the values in the ORDER
If the sales column had repeated values, RANGE would treat rows with the same sales value as part of the same
frame.
Query:
SELECT
month,
sales,
SUM(sales) OVER (ORDER BY sales RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT RO
W) AS range_sum
FROM sales;
The RANGE frame uses sales values to determine the frame, not the row positions.
The result will consider all rows up to the current row where sales values are equal or lower.
Query:
SELECT
month,
sales,
SUM(sales) OVER (ORDER BY sales RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWIN
G) AS range_future_total
FROM sales;
Result:
month sales range_future_total
For Jan, includes all sales values greater than or equal to 100 .
For Feb, includes all sales values greater than or equal to 200 .
SQL 39
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW Current row and the one before it.
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING Current row and the next one.
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING Current row and all rows after it.
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW All rows up to the current row based on value in ORDER BY .
RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING Current row and all rows with values greater than it.
These clauses give you control over the rows considered in the window function calculation for each row in
your result set.
The WITH clause in SQL, also known as a Common Table Expression (CTE), is a way to create a temporary, named
result set that you can reference within a SELECT , INSERT , UPDATE , or DELETE statement. It is particularly helpful for
breaking down complex queries into simpler, more readable parts by defining intermediate results that can be used
later in the main query.
Syntax
WITH CTE_name AS (
-- Your "scratch paper" query here
SELECT column1, column2
FROM table
WHERE condition
)
-- Main query that uses the CTE
SELECT *
FROM CTE_name;
Q.1 Fetch employees who earn more than average salary of all employees
WITH average_salary AS
(SELECT CAST(avg(salary) AS SIGNED) as avg_salary
FROM emp)
SELECT*
FROM emp e, average_salary av
WHERE e.SALARY>av.avg_salary
OR
💡 When you alias a CTE (or table), the alias replaces the original name for that query scope.
Q.2 Find stores whose sales are higher than average sales across all stores
Using normal sql query
SQL 40
SELECT cast(AVG(total_sales_per_store) AS SIGNED) AS avg_sales_per_store
FROM (SELECT s.store_id, SUM(cost) AS total_sales_per_store
FROM sales s
GROUP BY store_id) x;
SELECT*
FROM (SELECT s.store_id, SUM(cost) AS total_sales_per_store
FROM sales s
GROUP BY store_id) total_sales
INNER JOIN (SELECT cast(AVG(total_sales_per_store) AS SIGNED) AS avg_sales_per_store
FROM (SELECT s.store_id, SUM(cost) AS total_sales_per_store
FROM sales s
GROUP BY store_id) x) avg_sales
ON total_sales.total_sales_per_store>avg_sales.avg_sales_per_store;
Using CTE
avg_sales(average_cost_of_stores) AS
(SELECT CAST(AVG(total_cost_of_stores) AS SIGNED) AS average_cost_of_stores
FROM total_sales)
SELECT*
FROM total_sales ts
INNER JOIN avg_sales av
ON ts.total_cost_of_stores>av.average_cost_of_stores;
Syntax
SELECT columns
FROM table1
WHERE
column = (SELECT columns FROM table2 WHERE condition);
Eg
SELECT*
FROM aditya.personal
WHERE city = (SELECT cid FROM city WHERE cityname = "Agra");
1. Anchor Member: The initial part of the CTE, which serves as the base case (the starting point for recursion).
SQL 41
2. Recursive Member: A query that refers to the CTE itself, which allows it to call itself repeatedly until a stopping
condition is met.
The recursive CTE runs the anchor member first, which gets the initial set of rows. Then, it runs the recursive
member repeatedly, each time with the results from the previous iteration, until no new rows are added.
SELECT*
FROM cte_name;
SELECT*
FROM numbers;
Q. Write a sql query for for finding the employee hierarchy starting from Asha
This is the existing table:
SQL 42
JOIN emp_details D2
ON D2.id = H2.manager_id;
7 Asha Shripadh 1
5 Michael Asha 2
6 Arvind Asha 2
2 Satya Michael 3
3 Jia Michael 3
4 David Michael 3
Procedure in SQL
DELIMITER $$
create procedure pr_buy_products()
begin
declare v_product_code varchar(20);
declare v_price int;
SQL 43
from products
where product_name = 'iPhone 13 Pro Max';
update products
set quantity_remaining = (quantity_remaining - 1)
, quantity_sold = (quantity_sold + 1)
where product_code = v_product_code;
call pr_buy_products()
Questions
Question 1
Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last
characters. Your result cannot contain duplicates.
REGEXP '^[AEIOUaeiou].*[AEIOUaeiou]$':
^ asserts the start of the string.
[AEIOUaeiou] matches any vowel at the start or end.
.* matches any characters in between.
$ asserts the end of the string.
Question 2
Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
Question 3
Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your
result cannot contain duplicates.
Question 4
Query the Name of any student in STUDENTS who scored higher than 75 Marks. Order your output by the last
three characters of each name. If two or more students both have names ending in the same last three
characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.
SQL 44
SELECT Name
FROM STUDENTS
WHERE Marks > 75
ORDER BY RIGHT (Name, 3), ID;
Question 5
Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table,
but did not realize her keyboard's key was broken until after completing the calculation. She wants your help
finding the difference between her miscalculation (using salaries with any zeros removed), and the actual
average salary.
Write a query calculating the amount of error (i.e.: average monthly salaries), and round it up to the next
integer. The EMPLOYEES table is described.
SELECT
CEIL(AVG(Salary) - AVG(CAST(REPLACE(Salary, '0', '') AS UNSIGNED))) AS error
FROM EMPLOYEES;
Join Question
Question 1
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.
Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).
Return the result table in any order.
SELECT w1.id
FROM Weather AS w1
INNER JOIN Weather AS w2
ON DATEDIFF(w1.recordDate, w2.recordDate) = 1
WHERE w1.temperature>w2.temperature;
EY questions
Q.1 Write an sql query to find the count of three consecutive numbers.
Table
SQL 45
(5, 1),
(6, 2),
(7, 2);
Code
WITH cte AS
(SELECT num,
lead(num, 1) over() AS 2nd_num,
lead(num, 2) over() as 3rd_num
FROM logs )
Q.2 Write a sql query to find the average revenue by each sector on a monthly basis
Table
--Sectors
WITH cte AS
(SELECT*,
CASE
WHEN transaction_date BETWEEN '2020-01-01' AND '2020-01-31' THEN 1
WHEN transaction_date BETWEEN '2020-02-01' AND '2020-02-28' THEN 2
END AS month
FROM transactions)
SELECT DISTINCT c.month, s.sector, AVG(revenue) OVER(partition by month, sector) AS
FROM cte c
LEFT JOIN Sectors s
ON s.company_id = c.company_id
SQL 46
Zomato Questions
Find city wise customers count who have placed more than three orders in November 2023.
WITH cte AS (
SELECT customer_id, city, order_date, COUNT(order_id) OVER(PARTITION BY customer_id) A
FROM zomato_orders)
SELECT DISTINCT customer_id, city, no_of_orders
FROM cte
WHERE no_of_orders>3 and YEAR(order_date) = 2023
SQL 47