SQL YouTube Course Notes The iScale
SQL YouTube Course Notes The iScale
founders
1
Part 1
Unit – 01 Introduction to SQL-What Is SQL & Database
Unit – 02 Data Types, Primary-Foreign Keys & Constraints
Unit – 03 Create Table In SQL & Create Database
Unit – 04 INSERT UPDATE, DELETE & ALTER Table
Unit – 05 SELECT Statement & WHERE Clause
Unit – 06 How To Import Excel File (CSV) to SQL
Unit – 07 Functions in SQL & String Function
Unit – 08 Aggregate Functions –Types & Syntax
Unit – 09 Assignment Level 1 for SQL
Unit – 10 Practice & Test Questions Part 1
Part 2
Unit – 11 Group By and Having Clause
Unit – 12 Time Stamp and Extract Function, Date Time Function
Unit – 13 SQL JOINS –Types & Syntax
Unit – 14 SELF JOIN, UNION & UNION ALL
Unit – 15 Subquery
Unit – 16 Window Function –Types & Syntax
Unit – 17 Case Statement/Expression with examples
Unit – 18 CTE-Common Table Expression with examples
Unit – 19 Assignment Level 2 for SQL
Unit – 20 Practice & Test Questions Part 2
SQL By Nishant Dhote || www.theiscale.com 2
Unit - 1
INTRODUCTION TO SQL-WHAT IS SQL &
DATABASE-INTRODUCTION
• What is Database
• Excel v/s Database in SQL
• What is SQL
• It’s applications
• SQL v/s NoSQL
• Types of SQL Commands
Check the description box or comment section for the latest PostgreSQL installation video
Syntax
CREATE TABLE table_name
(
column_name1 datatypeconstraint,
column_name2 datatypeconstraint,
column_name3 datatypeconstraint,
);
Example
CREATE TABLE customer
(
CustID int8 PRIMARY KEY,
CustName varchar(50) NOT NULL,
Age int NOT NULL,
City char(50),
Salary numeric
);
SQL By Nishant Dhote || www.theiscale.com 24
Unit - 4
INSERT, UPDATE, DELETE VALUES IN TABLE
WITH ALTER, DROP & TRUNCATE TABLE
Example
INSERT INTO customer
(CustID, CustName, Age, City, Salary)
VALUES
(1, 'Nishant', 26, 'Delhi', 19000),
(2, 'Swati', 22, 'Bangalore', 11000),
(3, 'Ragini', 19, 'Goa', 24000),
(4, 'Saurabh', 32, 'Pune', 10000);
Example
UPDATE customer
SET CustName= 'Prashant' , Age= 32
WHERE CustID= 1;
Syntax
DELETE FROM table_nameWHERE condition;
Example
DELETE FROM customer
WHERE CustID= 3;
• ALTER/MODIFY COLUMN Syntax: changing Gender column data type from varchar(10) to char(10)
ALTER TABLE customer
ALTER COLUMN Gender char(10);
• Syntax
DROP TABLE table_name;
The TRUNCATE TABLE command deletes the data inside a table, but not the
table itself
Syntax
TRUNCATE TABLE table_name;
• Syntax
SELECT column_name FROM table_name;
To select all the fields available in the table
• Syntax
SELECT * FROM table_name;
To select distinct/unique fields available in the table
• Syntax
SELECT DISTINCT Column_name FROM table_name;
• Syntax
• Example
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
COPY
customer(customer_id,first_name,last_name,email,address_id)
FROM 'E:\customer.csv'
DELIMITER ','
CSV HEADER;
Types of Function:
1. System Defined Function : these are built-in functions
- Example: rand(), round(), upper(), lower(), count(), sum(), avg(), max(), etc
2. User-Defined Function : Once you define a function, you can call it in the same way
as the built-in functions
COPY accounts(customer_id,amount,mode,payment_date)
FROM 'E:\accounts.csv'
DELIMITER ','
CSV HEADER;
OR
- Syntax
SELECT column_name(s)
FROM table_name
GROUP BY column_name(s);
- Example
• SHOW TIMEZONE
• SELECT NOW()
• SELECT TIMEOFDAY()
• SELECT CURRENT_TIME
• SELECT CURRENT_DATE
SQL By Nishant Dhote || www.theiscale.com 56
EXTRACT FUNCTION
The EXTRACT() function extracts a part from a given date value.
• Example
SELECT *
FROM customer AS c
INNER JOIN accounts AS a
ON c.customer_id= a.customer_id
Example
SELECT *
FROM customer AS c
LEFT JOIN accounts AS a
ON c.customer_id= a.customer_id
Example
SELECT *
FROM customer AS c
RIGHT JOIN accounts AS a
ON c.customer_id= a.customer_id
Example
SELECT *
FROM customer AS c
FULL JOIN accounts AS a
ON c.customer_id= a.customer_id
Syntax
SELECT *
FROM classroom AS T1
JOIN classroom AS T2
ON T2.rollno = T1.st_id
Syntax
SELECT column_name(s) FROM TableA
UNION
SELECT column_name(s) FROM TableB
Example
SELECT customer_id
FROM accounts
UNION
SELECT customer_id
FROM customer
SQL By Nishant Dhote || www.theiscale.com 76
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 customer_id
FROM accounts
UNION ALL
SELECT customer_id
FROM customer
Syntax
SELECT column_name(s)
FROM table_name
WHERE column_nameoperator
( SELECT column_nameFROM table_nameWHERE ...);
SELECT *
FROM accounts
WHERE amount > 57
SELECT *
FROM accounts
WHERE amount > (SELECT avg(amount) from accounts)
- And OVER clause is used with window functions to define that window.
Define a Window
• PARTITION BY
• ORDER BY
• ROWS
Example:
SELECT customer_id , amount,
CASE
WHEN amount > 55 THEN 'Expensive'
WHEN amount = 55 THEN 'Less Expensive'
ELSE 'No Expensive'
END AS Show_Status
FROM accounts
SQL By Nishant Dhote || www.theiscale.com 93
CASE EXPRESSION
Syntax:
CASE EXPRESSION
WHEN value1 THEN result1
WHEN value2 THEN result2
WHEN valueN THEN resultN
ELSE other_result
END;
Example:
SELECT customer_id, amount,
CASE amount
WHEN 55 THEN 'Prime Customer'
WHEN 45 THEN 'Plus Customer'
ELSE 'Regular Customer'
END AS CustomerStatus
FROM accounts
OR