SQL Notebook by Rishabh
SQL Notebook by Rishabh
By Rishabh Mishra
1
Complete SQL With Notes
1. Introduction to SQL-What Is SQL & Database
2. Data Types, Primary-Foreign Keys & Constraints
a. Install postgresql and pgadmin4
3. Create Table In SQL & Create Database
4. INSERT UPDATE, DELETE & ALTER Table
5. SELECT Statement & WHERE Clause with Example
6. How To Import Excel File (CSV) to SQL
7. Functions in SQL & String Function
8. Aggregate Functions – Types & Syntax
9. Group By and Having Clause
10. Time Stamp and Extract Function, Date Time Function
11. SQL JOINS – Types & Syntax
12. SELF JOIN, UNION & UNION ALL
13. Subquery
14. Window Function – Types & Syntax
15. Case Statement/Expression with examples
16. CTE- Common Table Expression with examples
SQL By Rishabh Mishra 2
WHAT IS SQL & DATABASE-
INTRODUCTION
SQL Tutorial In Hindi-1
Low performance with huge volumes of data Easily work with huge volumes of data
Good for one time analysis, quick charts Can automate tasks
YouTube: https://www.youtube.com/@RishabhMishraOfficial
12
DATA TYPES, PRIMARY &
FOREIGN KEYS, CONSTRAINTS
SQL Tutorial In Hindi-2
RDBMS
Example
• Example
CREATE TABLE customer
(
CustID int8 PRIMARY KEY,
CustName varchar(50) NOT NULL,
Age int NOT NULL,
City char(50),
Salary numeric
);
SQL By Rishabh Mishra 24
Insert, Update, Delete
Values in Table
+
Alter, Drop & Truncate Table
SQL Tutorial In Hindi-4
• Example
UPDATE customer
SET CustName = 'Xam', Age= 32
WHERE CustID = 4;
• Example
DELETE FROM customer
WHERE CustID = 3;
The TRUNCATE TABLE command deletes the data inside a table, but
not the table itself
• Syntax
TRUNCATE TABLE table_name;
• Example
SELECT name FROM classroom
WHERE grade=‘A’;
ORDER BY Clause
The ORDER BY is used to sort the result-set in ascending (ASC) or descending
order (DESC).
Example: below code will sort the output data by column name in ascending order
SELECT column_name FROM table_name
ORDER BY column_name e ASC;
SQL By Rishabh Mishra 37
IMPORT CSV FILE
SQL Tutorial In Hindi-6
• Example
SELECT mode, SUM(amount) AS total
FROM payment
GROUP BY mode
?
Answer in video’s comment
(no cheating)
SQL By Rishabh Mishra 48
TIMESTAMPS & EXTRACT
SQL Tutorial In Hindi-10
payment
customer_id
amount
customer mode
customer_id Payment_date
first_name
last_name country
address_id address city_id Database
city
address_id
country
address
city_id
postal_code
phone
SQL By Rishabh Mishra 56
JOIN Example
Question: How much amount was
paid by customer ‘Madan’, what
was mode and payment date?
• Example
SELECT *
FROM customer AS c
INNER JOIN payment AS p
ON c.customer_id = p.customer_id
SQL By Rishabh Mishra 60
LEFT JOIN
• Returns all records from the
left table, and the matched
records from the right table
• Example
SELECT *
FROM customer AS c
LEFT JOIN payment AS p
ON c.customer_id = p.customer_id
SQL By Rishabh Mishra 62
RIGHT JOIN
• Returns all records from the
right table, and the matched
records from the left table
• Example
SELECT *
FROM customer AS c
RIGHT JOIN payment AS p
ON c.customer_id = p.customer_id
SQL By Rishabh Mishra 64
FULL JOIN
• Returns all records when
there is a match in either left
or right table
• Example
SELECT *
FROM customer AS c
FULL OUTER JOIN payment AS p
ON c.customer_id = p.customer_id
SQL By Rishabh Mishra 66
Which JOIN To Use
• INNER JOIN: Returns records that have matching values in both tables
• LEFT JOIN: Returns all records from the left table, and the matched
records from the right table
• RIGHT JOIN: Returns all records from the right table, and the matched
records from the left table
• FULL JOIN: Returns all records when there is a match in either left or
right table
Table: emp
• Example
SELECT cust_name, cust_amount from custA
UNION
SELECT cust_name, cust_amount from custB
SQL By Rishabh Mishra 73
UNION ALL
In UNION ALL everything is same as UNION, it
combines/concatenate two or more table but keeps all
records, including duplicates
• Syntax
SELECT column_name(s) FROM TableA
UNION ALL
SELECT column_name(s) FROM TableB
• Example
SELECT cust_name, cust_amount from custA
UNION ALL
SELECT cust_name, cust_amount from custB
SQL By Rishabh Mishra 74
UNION Example
Table: custA Table: custB
• Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator
( SELECT column_name FROM table_name WHERE ... );
SQL By Rishabh Mishra 77
SUB QUERY Example
Question: Find the details of customers, whose payment
amount is more than the average of total amount paid by all
customers
Give output one row per aggregation The rows maintain their separate identities
NOTE: Above we have used: “ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING”
which will give a SINGLE output based on all INPUT Values/PARTITION
SQL By Rishabh Mishra(if used) 86
SELECT new_id, RANKING
ROW_NUMBER() OVER(ORDER BY new_id) AS "ROW_NUMBER", FUNCTION
Example
RANK() OVER(ORDER BY new_id) AS "RANK",
DENSE_RANK() OVER(ORDER BY new_id) AS "DENSE_RANK",
PERCENT_RANK() OVER(ORDER BY new_id) AS "PERCENT_RANK"
FROM test_data
Instagram: https://www.instagram.com/rishabhnmishra/
LinkedIn: https://www.linkedin.com/in/rishabhnmishra/
By Rishabh Mishra
102