What will this query return?
SELECT name FROM employees;
SELECT name FROM employees;
Anonymous Quiz
8%
A. All columns
88%
B. Only names of employees
1%
C. Only salaries
3%
D. Table structure
β€2
What does this query do?
SELECT * FROM employees WHERE department = 'HR';
SELECT * FROM employees WHERE department = 'HR';
Anonymous Quiz
6%
A. Shows all employees
90%
B. Shows only HR employees
2%
C. Deletes HR employees
2%
D. Updates HR employees
β€2π₯°1
Which operator is used to combine multiple conditions?
Anonymous Quiz
51%
A. JOIN
43%
B. AND
3%
C. SELECT
2%
D. ORDER
β€3
Now, Letβs move to the next topic of the SQL Roadmap:
ORDER BY LIMIT
π§ 1. ORDER BY (Sorting Data)
ORDER BY is used to sort your result.
π Syntax
πΉ Ascending Order (Default)
β Lowest salary β highest
πΉ Descending Order
β Highest salary β lowest
π‘ 2. Sorting Multiple Columns
π First sorts by department
π Then salary within each department
π― 3. LIMIT (Control Output Size)
LIMIT is used to restrict the number of rows.
π Syntax
π Example
β Returns only the first 5 rows
β‘ 4. Using ORDER BY LIMIT
π Top 5 highest salaries
π Lowest 3 salaries
π― 5. Practice Tasks
1. Show all employees sorted by salary (ascending)
2. Show all employees sorted by salary (descending)
3. Get top 3 highest paid employees
4. Get lowest 2 salary employees
5. Sort employees by department and salary
β Practice Task Solution
β 1. Show all employees sorted by salary (ascending)
β 2. Show all employees sorted by salary (descending)
β 3. Get top 3 highest paid employees
β 4. Get lowest 2 salary employees
β 5. Sort employees by department and salary
π First sorts by department
π Then highest salary inside each department
β‘ Mini Challenge π₯
π Get the 2nd highest salary employee.
β‘ Mini Challenge Solution π₯
β Method 1 (Using LIMIT + OFFSET)
β Method 2 (Alternative way)
π₯ Pro Tip:
If you understand OFFSET β you can get Top N, 2nd highest, 3rd highest easily.
β‘ Double Tap β€οΈ For More
ORDER BY LIMIT
π§ 1. ORDER BY (Sorting Data)
ORDER BY is used to sort your result.
π Syntax
SELECT column_name FROM table_name
ORDER BY column_name;
πΉ Ascending Order (Default)
SELECT * FROM employees
ORDER BY salary ASC;
β Lowest salary β highest
πΉ Descending Order
SELECT * FROM employees
ORDER BY salary DESC;
β Highest salary β lowest
π‘ 2. Sorting Multiple Columns
SELECT * FROM employees
ORDER BY department ASC, salary DESC;
π First sorts by department
π Then salary within each department
π― 3. LIMIT (Control Output Size)
LIMIT is used to restrict the number of rows.
π Syntax
SELECT * FROM table_name
LIMIT number;
π Example
SELECT * FROM employees
LIMIT 5;
β Returns only the first 5 rows
β‘ 4. Using ORDER BY LIMIT
π Top 5 highest salaries
SELECT * FROM employees
ORDER BY salary DESC
LIMIT 5;
π Lowest 3 salaries
SELECT * FROM employees
ORDER BY salary ASC
LIMIT 3;
π― 5. Practice Tasks
1. Show all employees sorted by salary (ascending)
2. Show all employees sorted by salary (descending)
3. Get top 3 highest paid employees
4. Get lowest 2 salary employees
5. Sort employees by department and salary
β Practice Task Solution
β 1. Show all employees sorted by salary (ascending)
SELECT * FROM employees
ORDER BY salary ASC;
β 2. Show all employees sorted by salary (descending)
SELECT * FROM employees
ORDER BY salary DESC;
β 3. Get top 3 highest paid employees
SELECT * FROM employees
ORDER BY salary DESC
LIMIT 3;
β 4. Get lowest 2 salary employees
SELECT * FROM employees
ORDER BY salary ASC
LIMIT 2;
β 5. Sort employees by department and salary
SELECT * FROM employees
ORDER BY department ASC, salary DESC;
π First sorts by department
π Then highest salary inside each department
β‘ Mini Challenge π₯
π Get the 2nd highest salary employee.
β‘ Mini Challenge Solution π₯
β Method 1 (Using LIMIT + OFFSET)
SELECT * FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
β Method 2 (Alternative way)
SELECT * FROM employees
ORDER BY salary DESC
LIMIT 1, 1;
π₯ Pro Tip:
If you understand OFFSET β you can get Top N, 2nd highest, 3rd highest easily.
β‘ Double Tap β€οΈ For More
β€23
π₯ Letβs move to the next topic in the SQL Roadmap:
β GROUP BY & Aggregation Functions
π§ 1. What is GROUP BY?
GROUP BY is used to group rows with same values
π It helps you summarize data
π‘ Example Table: employees
name department salary
Amit IT 60000
Neha HR 40000
Ravi IT 70000
Sara HR 50000
π Without GROUP BY
SELECT AVG(salary) FROM employees;
β Gives overall average
π With GROUP BY
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
β Gives average salary per department
β‘ 2. Aggregation Functions
These functions perform calculations on data
πΉ COUNT() β number of rows
SELECT COUNT() FROM employees;
πΉ SUM() β total
SELECT SUM(salary) FROM employees;
πΉ AVG() β average
SELECT AVG(salary) FROM employees;
πΉ MIN() β smallest value
SELECT MIN(salary) FROM employees;
πΉ MAX() β largest value
SELECT MAX(salary) FROM employees;
π― 3. GROUP BY + Aggregation
π Count employees in each department
SELECT department, COUNT()
FROM employees
GROUP BY department;
π Total salary per department
SELECT department, SUM(salary)
FROM employees
GROUP BY department;
π Highest salary per department
SELECT department, MAX(salary)
FROM employees
GROUP BY department;
π¨ 4. Important Rule (Interview Favorite)
π Every column in SELECT must be:
- Either inside GROUP BY
- Or used with aggregation function
β Wrong:
SELECT name, AVG(salary) FROM employees;
β Correct:
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
π― 5. Practice Tasks
1. Count total employees
2. Find total salary of all employees
3. Find average salary per department
4. Find maximum salary in each department
5. Count employees in each department
β Practice Task Solution
β 1. Count total employees
SELECT COUNT() FROM employees;
β 2. Find total salary of all employees
SELECT SUM(salary) FROM employees;
β 3. Find average salary per department
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
β 4. Find maximum salary in each department
SELECT department, MAX(salary)
FROM employees
GROUP BY department;
β 5. Count employees in each department
SELECT department, COUNT()
FROM employees
GROUP BY department;
β‘ Mini Challenge π₯
π Find department with highest average salary
β‘ Mini Challenge Solution π₯
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC
LIMIT 1;
β‘ Double Tap β€οΈ For More
β GROUP BY & Aggregation Functions
π§ 1. What is GROUP BY?
GROUP BY is used to group rows with same values
π It helps you summarize data
π‘ Example Table: employees
name department salary
Amit IT 60000
Neha HR 40000
Ravi IT 70000
Sara HR 50000
π Without GROUP BY
SELECT AVG(salary) FROM employees;
β Gives overall average
π With GROUP BY
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
β Gives average salary per department
β‘ 2. Aggregation Functions
These functions perform calculations on data
πΉ COUNT() β number of rows
SELECT COUNT() FROM employees;
πΉ SUM() β total
SELECT SUM(salary) FROM employees;
πΉ AVG() β average
SELECT AVG(salary) FROM employees;
πΉ MIN() β smallest value
SELECT MIN(salary) FROM employees;
πΉ MAX() β largest value
SELECT MAX(salary) FROM employees;
π― 3. GROUP BY + Aggregation
π Count employees in each department
SELECT department, COUNT()
FROM employees
GROUP BY department;
π Total salary per department
SELECT department, SUM(salary)
FROM employees
GROUP BY department;
π Highest salary per department
SELECT department, MAX(salary)
FROM employees
GROUP BY department;
π¨ 4. Important Rule (Interview Favorite)
π Every column in SELECT must be:
- Either inside GROUP BY
- Or used with aggregation function
β Wrong:
SELECT name, AVG(salary) FROM employees;
β Correct:
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
π― 5. Practice Tasks
1. Count total employees
2. Find total salary of all employees
3. Find average salary per department
4. Find maximum salary in each department
5. Count employees in each department
β Practice Task Solution
β 1. Count total employees
SELECT COUNT() FROM employees;
β 2. Find total salary of all employees
SELECT SUM(salary) FROM employees;
β 3. Find average salary per department
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
β 4. Find maximum salary in each department
SELECT department, MAX(salary)
FROM employees
GROUP BY department;
β 5. Count employees in each department
SELECT department, COUNT()
FROM employees
GROUP BY department;
β‘ Mini Challenge π₯
π Find department with highest average salary
β‘ Mini Challenge Solution π₯
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC
LIMIT 1;
β‘ Double Tap β€οΈ For More
β€22
Now, letβs move to the next topic of SQL Roadmap:
β HAVING Clause
π§ 1. What is HAVING?
HAVING is used to filter grouped data
π Think like this:
- WHERE β filters rows (before grouping)
- HAVING β filters groups (after grouping)
β‘ 2. WHERE vs HAVING
Works on
- WHERE: Rows
- HAVING: Groups
Used with
- WHERE: SELECT
- HAVING: GROUP BY
Can use aggregates?
- WHERE: β No
- HAVING: β Yes
π‘ 3. Basic Syntax
SELECT column, AGG_FUNCTION(column)
FROM table_name
GROUP BY column
HAVING condition;
π― 4. Examples (Very Important)
π Example 1: Departments with more than 2 employees
SELECT department, COUNT() AS total_emp
FROM employees
GROUP BY department
HAVING COUNT() > 2;
π Example 2: Departments with average salary > 50k
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
β‘ 5. WHERE + HAVING Together
SELECT department, COUNT() AS total_emp
FROM employees
WHERE salary > 30000
GROUP BY department
HAVING COUNT() > 2;
π Step-by-step:
1. WHERE β filters employees with salary > 30k
2. GROUP BY β groups by department
3. HAVING β keeps only groups with > 2 employees
π― 6. Practice Tasks
1. Find departments having more than 3 employees
2. Find departments with average salary > 60k
3. Count employees per department where salary > 40k
4. Show departments with total salary > 1,00,000
5. Find departments with minimum salary > 30k
β‘ Mini Challenge π₯
π Find departments having more than 2 employees AND avg salary > 50k
Double Tap β€οΈ For Practice Task Solution
β HAVING Clause
π§ 1. What is HAVING?
HAVING is used to filter grouped data
π Think like this:
- WHERE β filters rows (before grouping)
- HAVING β filters groups (after grouping)
β‘ 2. WHERE vs HAVING
Works on
- WHERE: Rows
- HAVING: Groups
Used with
- WHERE: SELECT
- HAVING: GROUP BY
Can use aggregates?
- WHERE: β No
- HAVING: β Yes
π‘ 3. Basic Syntax
SELECT column, AGG_FUNCTION(column)
FROM table_name
GROUP BY column
HAVING condition;
π― 4. Examples (Very Important)
π Example 1: Departments with more than 2 employees
SELECT department, COUNT() AS total_emp
FROM employees
GROUP BY department
HAVING COUNT() > 2;
π Example 2: Departments with average salary > 50k
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
β‘ 5. WHERE + HAVING Together
SELECT department, COUNT() AS total_emp
FROM employees
WHERE salary > 30000
GROUP BY department
HAVING COUNT() > 2;
π Step-by-step:
1. WHERE β filters employees with salary > 30k
2. GROUP BY β groups by department
3. HAVING β keeps only groups with > 2 employees
π― 6. Practice Tasks
1. Find departments having more than 3 employees
2. Find departments with average salary > 60k
3. Count employees per department where salary > 40k
4. Show departments with total salary > 1,00,000
5. Find departments with minimum salary > 30k
β‘ Mini Challenge π₯
π Find departments having more than 2 employees AND avg salary > 50k
Double Tap β€οΈ For Practice Task Solution
β€18
β
Business Intelligence (BI) Acronyms You Should Know ππ‘
BI β Business Intelligence
ETL β Extract, Transform, Load
ELT β Extract, Load, Transform
DWH β Data Warehouse
OLAP β Online Analytical Processing
OLTP β Online Transaction Processing
KPI β Key Performance Indicator
SLA β Service Level Agreement
SCD β Slowly Changing Dimension
CDC β Change Data Capture
MDM β Master Data Management
EAV β Entity Attribute Value
FACT β Fact Table
DIM β Dimension Table
STAR β Star Schema
SNOWFLAKE β Snowflake Schema
MTD β Month To Date
QTD β Quarter To Date
YTD β Year To Date
MoM β Month over Month
YoY β Year over Year
ROI β Return on Investment
TAT β Turn Around Time
π‘Donβt just expand acronyms β explain where theyβre used (ETL in pipelines, KPIs in dashboards, OLAP in analysis).
π¬ Tap β€οΈ for more!
BI β Business Intelligence
ETL β Extract, Transform, Load
ELT β Extract, Load, Transform
DWH β Data Warehouse
OLAP β Online Analytical Processing
OLTP β Online Transaction Processing
KPI β Key Performance Indicator
SLA β Service Level Agreement
SCD β Slowly Changing Dimension
CDC β Change Data Capture
MDM β Master Data Management
EAV β Entity Attribute Value
FACT β Fact Table
DIM β Dimension Table
STAR β Star Schema
SNOWFLAKE β Snowflake Schema
MTD β Month To Date
QTD β Quarter To Date
YTD β Year To Date
MoM β Month over Month
YoY β Year over Year
ROI β Return on Investment
TAT β Turn Around Time
π‘Donβt just expand acronyms β explain where theyβre used (ETL in pipelines, KPIs in dashboards, OLAP in analysis).
π¬ Tap β€οΈ for more!
β€18π2π₯1π₯°1
What is the purpose of HAVING clause?
Anonymous Quiz
31%
A. Filter rows before grouping
60%
B. Filter grouped data
6%
C. Sort data
2%
D. Join tables
Which clause is used before GROUP BY?
Anonymous Quiz
28%
A. HAVING
19%
B. ORDER BY
50%
C. WHERE
2%
D. LIMIT
What will this query return?
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 2;
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 2;
Anonymous Quiz
82%
Departments with more than 2 employees
13%
Departments with less than 2 employees
4%
All departments
1%
Highest salary
β€5
What does this query return?
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;
Anonymous Quiz
5%
All departments
92%
Departments with avg salary > 50k
3%
Max salary
0%
Min salary
β€8
Top WhatsApp channels for Free Learning ππ
Free Courses with Certificate: https://whatsapp.com/channel/0029Vamhzk5JENy1Zg9KmO2g
Data Analysts: https://whatsapp.com/channel/0029VaGgzAk72WTmQFERKh02
MS Excel: https://whatsapp.com/channel/0029VaifY548qIzv0u1AHz3i
Jobs & Internship Opportunities:
https://whatsapp.com/channel/0029VaI5CV93AzNUiZ5Tt226
Web Development: https://whatsapp.com/channel/0029VaiSdWu4NVis9yNEE72z
Python Free Books & Projects: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L
Java Resources: https://whatsapp.com/channel/0029VamdH5mHAdNMHMSBwg1s
Coding Interviews: https://whatsapp.com/channel/0029VammZijATRSlLxywEC3X
SQL: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Power BI: https://whatsapp.com/channel/0029Vai1xKf1dAvuk6s1v22c
Programming Free Resources: https://whatsapp.com/channel/0029VahiFZQ4o7qN54LTzB17
Data Science Projects: https://whatsapp.com/channel/0029Va4QUHa6rsQjhITHK82y
Learn Data Science & Machine Learning: https://whatsapp.com/channel/0029Va8v3eo1NCrQfGMseL2D
Improve your communication skills: https://whatsapp.com/channel/0029VaiaucV4NVik7Fx6HN2n
Learn Ethical Hacking and Cybersecurity: https://whatsapp.com/channel/0029VancSnGG8l5KQYOOyL1T
Donβt worry Guys your contact number will stay hidden!
ENJOY LEARNING ππ
Free Courses with Certificate: https://whatsapp.com/channel/0029Vamhzk5JENy1Zg9KmO2g
Data Analysts: https://whatsapp.com/channel/0029VaGgzAk72WTmQFERKh02
MS Excel: https://whatsapp.com/channel/0029VaifY548qIzv0u1AHz3i
Jobs & Internship Opportunities:
https://whatsapp.com/channel/0029VaI5CV93AzNUiZ5Tt226
Web Development: https://whatsapp.com/channel/0029VaiSdWu4NVis9yNEE72z
Python Free Books & Projects: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L
Java Resources: https://whatsapp.com/channel/0029VamdH5mHAdNMHMSBwg1s
Coding Interviews: https://whatsapp.com/channel/0029VammZijATRSlLxywEC3X
SQL: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Power BI: https://whatsapp.com/channel/0029Vai1xKf1dAvuk6s1v22c
Programming Free Resources: https://whatsapp.com/channel/0029VahiFZQ4o7qN54LTzB17
Data Science Projects: https://whatsapp.com/channel/0029Va4QUHa6rsQjhITHK82y
Learn Data Science & Machine Learning: https://whatsapp.com/channel/0029Va8v3eo1NCrQfGMseL2D
Improve your communication skills: https://whatsapp.com/channel/0029VaiaucV4NVik7Fx6HN2n
Learn Ethical Hacking and Cybersecurity: https://whatsapp.com/channel/0029VancSnGG8l5KQYOOyL1T
Donβt worry Guys your contact number will stay hidden!
ENJOY LEARNING ππ
β€13
π₯Now, letβs move to the most important SQL topic β JOINS π―π₯
π§ 1. What is a JOIN?
JOIN is used to combine data from multiple tables
π If data is stored in different tables β JOIN helps you connect them
π Example Tables
π¨βπΌ employees
emp_id name dept_id
1 Amit 101
2 Neha 102
3 Ravi 101
π’ departments
dept_id dept_name
101 IT
102 HR
π 2. INNER JOIN (Most Used π₯)
π Returns only matching records
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;
β Only employees with valid department
β¬ οΈ 3. LEFT JOIN
π Returns all records from left table + matched from right
SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
β Even if department is missing β employee will still show
β‘οΈ 4. RIGHT JOIN
π Returns all records from right table + matched from left
SELECT e.name, d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;
π 5. FULL JOIN
π Returns all records from both tables
SELECT e.name, d.dept_name
FROM employees e
FULL JOIN departments d
ON e.dept_id = d.dept_id;
(Note: Not supported in MySQL directly β use UNION instead)
β‘ 6. Quick Summary
β’ INNER -> Only matching rows
β’ LEFT -> All left + matched right
β’ RIGHT -> All right + matched left
β’ FULL -> Everything
π― 7. Practice Tasks
1. Get employee name with department name
2. Show all employees even if no department
3. Show all departments even if no employees
4. Find employees without department
5. Count employees per department (using JOIN)
π₯ Practice Task Solutions π
β 1. Get employee name with department name (INNER JOIN)
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;
β 2. Show all employees even if no department (LEFT JOIN)
SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
β 3. Show all departments even if no employees (RIGHT JOIN)
SELECT e.name, d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;
π (Alternative using LEFT JOIN β interview friendly)
SELECT e.name, d.dept_name
FROM departments d
LEFT JOIN employees e
ON e.dept_id = d.dept_id;
β 4. Find employees without department
SELECT e.name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id
WHERE d.dept_id IS NULL;
β 5. Count employees per department (using JOIN)
SELECT d.dept_name, COUNT(e.emp_id) AS total_emp
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
GROUP BY d.dept_name;
β‘ Mini Challenge π₯
π Find departments with no employees
β‘ Mini Challenge Solution π
Double Tap β€οΈ For More
π§ 1. What is a JOIN?
JOIN is used to combine data from multiple tables
π If data is stored in different tables β JOIN helps you connect them
π Example Tables
π¨βπΌ employees
emp_id name dept_id
1 Amit 101
2 Neha 102
3 Ravi 101
π’ departments
dept_id dept_name
101 IT
102 HR
π 2. INNER JOIN (Most Used π₯)
π Returns only matching records
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;
β Only employees with valid department
β¬ οΈ 3. LEFT JOIN
π Returns all records from left table + matched from right
SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
β Even if department is missing β employee will still show
β‘οΈ 4. RIGHT JOIN
π Returns all records from right table + matched from left
SELECT e.name, d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;
π 5. FULL JOIN
π Returns all records from both tables
SELECT e.name, d.dept_name
FROM employees e
FULL JOIN departments d
ON e.dept_id = d.dept_id;
(Note: Not supported in MySQL directly β use UNION instead)
β‘ 6. Quick Summary
β’ INNER -> Only matching rows
β’ LEFT -> All left + matched right
β’ RIGHT -> All right + matched left
β’ FULL -> Everything
π― 7. Practice Tasks
1. Get employee name with department name
2. Show all employees even if no department
3. Show all departments even if no employees
4. Find employees without department
5. Count employees per department (using JOIN)
π₯ Practice Task Solutions π
β 1. Get employee name with department name (INNER JOIN)
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;
β 2. Show all employees even if no department (LEFT JOIN)
SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
β 3. Show all departments even if no employees (RIGHT JOIN)
SELECT e.name, d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;
π (Alternative using LEFT JOIN β interview friendly)
SELECT e.name, d.dept_name
FROM departments d
LEFT JOIN employees e
ON e.dept_id = d.dept_id;
β 4. Find employees without department
SELECT e.name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id
WHERE d.dept_id IS NULL;
β 5. Count employees per department (using JOIN)
SELECT d.dept_name, COUNT(e.emp_id) AS total_emp
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
GROUP BY d.dept_name;
β‘ Mini Challenge π₯
π Find departments with no employees
β‘ Mini Challenge Solution π
SELECT d.dept_name
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
WHERE e.emp_id IS NULL;
Double Tap β€οΈ For More
β€23π1
What does INNER JOIN return?
Anonymous Quiz
14%
A. All rows from both tables
80%
B. Only matching rows from both tables
4%
C. Only left table rows
1%
D. Only right table rows
β€1
What will LEFT JOIN return?
Anonymous Quiz
4%
A. Only matching rows
92%
B. All rows from left + matching from right
4%
C. All rows from right
0%
D. Only unmatched rows
β€2
How to find records with NO match in JOIN?
Anonymous Quiz
14%
A. Use INNER JOIN
79%
B. Use WHERE column IS NULL
6%
C. Use GROUP BY
1%
D. Use ORDER BY
β€1
Find departments with NO employees β which is correct?
Anonymous Quiz
12%
SELECT d.name FROM departments d INNER JOIN employees e ON d.dept_id = e.dept_id;
77%
3%
SELECT * FROM employees;
8%
SELECT COUNT(*) FROM departments;
What is the difference?
-- Query 1 SELECT COUNT(*) FROM employees e INNER JOIN departments d ON e.dept_id = d.dept_id; -- Query 2 SELECT COUNT(*) FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id;
-- Query 1 SELECT COUNT(*) FROM employees e INNER JOIN departments d ON e.dept_id = d.dept_id; -- Query 2 SELECT COUNT(*) FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id;
Anonymous Quiz
34%
Query 1 β€ Query 2
38%
Query 1 β₯ Query 2
20%
Both same
7%
None of the above
What will this query return?
SELECT COUNT(*) FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id;
SELECT COUNT(*) FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id;
Anonymous Quiz
24%
Only matched rows
56%
Total employees count
17%
Total departments
2%
Employees name
β€3
What will this query return?
SELECT e.name, d.dept_name FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id WHERE d.dept_name = 'IT';
SELECT e.name, d.dept_name FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id WHERE d.dept_name = 'IT';
Anonymous Quiz
7%
All employees
10%
All departments
13%
Employees without department
69%
Only employees in IT
β€8
π₯ Now, letβs move to the next topic of SQL Roadmap
β Subqueries (Nested Queries)
π§ 1. What is a Subquery?
A subquery is a query inside another query
π Think like this:
βFirst get some data β then use that result in another queryβ
π Basic Example
π Find employees earning above average salary
SELECT FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
π Inner query β gives average salary
π Outer query β filters employees
β‘ 2. Types of Subqueries
πΉ Single Row Subquery
Returns only one value
SELECT FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
πΉ Multiple Row Subquery
Returns multiple values
SELECT FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments
);
π― 3. Subquery with IN
SELECT name
FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments WHERE dept_name = 'IT'
);
β Finds employees in IT department
β‘ 4. Subquery with EXISTS
SELECT name
FROM employees e
WHERE EXISTS (
SELECT 1 FROM departments d
WHERE e.dept_id = d.dept_id
);
β Checks if matching record exists
π¨ 5. Important Difference
IN ->
Compares values & Slower with large data
EXISTS -> Checks existence & Faster with large data
π― 6. Practice Tasks
1. Find employees with salary > average salary
2. Find employees in IT department using subquery
3. Get departments that have employees
4. Find employees with max salary
5. Get employees not in HR department
π₯ Here are the solutions for Subqueries practice tasks
β 1. Find employees with salary > average salary
SELECT FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
β 2. Find employees in IT department using subquery
SELECT FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments
WHERE dept_name = 'IT'
);
β 3. Get departments that have employees
SELECT FROM departments
WHERE dept_id IN (
SELECT dept_id FROM employees
);
π Alternative (using EXISTS):
SELECT FROM departments d
WHERE EXISTS (
SELECT 1 FROM employees e
WHERE d.dept_id = e.dept_id
);
β 4. Find employees with max salary
SELECT FROM employees
WHERE salary = (
SELECT MAX(salary) FROM employees
);
β 5. Get employees not in HR department
SELECT FROM employees
WHERE dept_id NOT IN (
SELECT dept_id FROM departments
WHERE dept_name = 'HR'
);
β‘ Mini Challenge π₯
π Find employees earning second highest salary using subquery
β‘ Mini Challenge Solution π₯
SELECT FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
)
);
π₯ Pro Tip:
If question says:
π βabove averageβ, βmaxβ, βsecond highestβ
β Think Subquery instantly π―
Double Tap β€οΈ For More
β Subqueries (Nested Queries)
π§ 1. What is a Subquery?
A subquery is a query inside another query
π Think like this:
βFirst get some data β then use that result in another queryβ
π Basic Example
π Find employees earning above average salary
SELECT FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
π Inner query β gives average salary
π Outer query β filters employees
β‘ 2. Types of Subqueries
πΉ Single Row Subquery
Returns only one value
SELECT FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
πΉ Multiple Row Subquery
Returns multiple values
SELECT FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments
);
π― 3. Subquery with IN
SELECT name
FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments WHERE dept_name = 'IT'
);
β Finds employees in IT department
β‘ 4. Subquery with EXISTS
SELECT name
FROM employees e
WHERE EXISTS (
SELECT 1 FROM departments d
WHERE e.dept_id = d.dept_id
);
β Checks if matching record exists
π¨ 5. Important Difference
IN ->
Compares values & Slower with large data
EXISTS -> Checks existence & Faster with large data
π― 6. Practice Tasks
1. Find employees with salary > average salary
2. Find employees in IT department using subquery
3. Get departments that have employees
4. Find employees with max salary
5. Get employees not in HR department
π₯ Here are the solutions for Subqueries practice tasks
β 1. Find employees with salary > average salary
SELECT FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
β 2. Find employees in IT department using subquery
SELECT FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments
WHERE dept_name = 'IT'
);
β 3. Get departments that have employees
SELECT FROM departments
WHERE dept_id IN (
SELECT dept_id FROM employees
);
π Alternative (using EXISTS):
SELECT FROM departments d
WHERE EXISTS (
SELECT 1 FROM employees e
WHERE d.dept_id = e.dept_id
);
β 4. Find employees with max salary
SELECT FROM employees
WHERE salary = (
SELECT MAX(salary) FROM employees
);
β 5. Get employees not in HR department
SELECT FROM employees
WHERE dept_id NOT IN (
SELECT dept_id FROM departments
WHERE dept_name = 'HR'
);
β‘ Mini Challenge π₯
π Find employees earning second highest salary using subquery
β‘ Mini Challenge Solution π₯
SELECT FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
)
);
π₯ Pro Tip:
If question says:
π βabove averageβ, βmaxβ, βsecond highestβ
β Think Subquery instantly π―
Double Tap β€οΈ For More
β€15