Inner Join:
Department table
Employee table
dept_id dept_name
10 HR
emp_id emp_name dept_id
30 Finance
1 Alice 10
2 Bob 20
3 Carol NULL
SELECT *
FROM Employees e
INNER JOIN Departments d ON e.dept_id = d.dept_id;
Output table-
emp_name dept_name
Alice HR
Left Join:
SELECT *
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.dept_id;
Employee table department table
emp_id emp_name dept_id
1 Alice 10
dept_id dept_name
2 Bob 20
10 HR
3 Carol NULL
30 Finance
Output table -
emp_name emp_id dept_name
(Output)
Alice 10 HR
Bob 20 NULL
Carol NULL NULL
Right Join:
SELECT *
FROM Employees e
RIGHT JOIN Departments d ON e.dept_id = d.dept_id;
Employee table department table
emp_id emp_name dept_id
1 Alice 10
dept_id dept_name
2 Bob 20
10 HR
3 Carol NULL
30 Finance
Output table–
emp_name emp_id dept_name
Alice 10 HR
NULL 30 Finance
Full outer Join :
SELECT *
FROM Employees e
FULL OUTER JOIN Departments d ON e.dept_id = d.dept_id;
Employee table department table
emp_id emp_name dept_id
1 Alice 10
dept_id dept_name
2 Bob 20
10 HR
3 Carol NULL
30 Finance
Output table:
emp_name emp_id dept_name
Alice 10 HR
Bob 20 NULL
Carol NULL NULL
NULL 30 Finance
Exercise 3
Perform left join operations for the given two tables.
Students table
Class table
stu_id name class_id
1 alice 101
class_id class_name
2 bob null
101 math
3 carol 102
102 science
4 david null
NULL Not assigned
Left join:
Output table-
alice math
bob null
Carol science
david null
Because NULL = NULL is not true in SQL!
So even though both tables have NULL in class_id, they don’t match.
Like operator–
● The percent sign % represents zero, one, or multiple characters
● The underscore sign _ represents one, single character
Return all customers that have "r" in the second position:
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
Exercise 4
1-Find Patients Whose Name Contains 'el'
2-Find Patients Whose Name Has Exactly 5 Letters
3-Find Patients Whose Name Starts With 'J' and Has 'n' in It
CTE (Common Table Expression)
A CTE (Common Table Expression) is a temporary, named result set that you can reference within a
SELECT, INSERT, UPDATE, or DELETE statement. It's especially useful for simplifying complex
queries or using recursion.
Basic syntax:
WITH cte_name AS (
SELECT column1, column2
FROM table_name
WHERE condition
)
SELECT *
FROM cte_name;
Example : Suppose we want to get employees with salaries above the average.
WITH AvgSalaryCTE AS (
SELECT AVG(Salary) AS avg_salary
FROM Employees
)
SELECT e.Name, e.Salary
FROM Employees e, AvgSalaryCTE
WHERE e.Salary > AvgSalaryCTE.avg_salary;
Exercise 6
Miscellaneous Problems
(Windows function + case when then + wildcard + CTE + basic sql
commands)
Ques 1 -
Show patient_id, first_name, last_name, and attending doctor's specialty.
Show only the patients who has a diagnosis as 'Epilepsy' and the doctor's first name
is 'Lisa'
select p.patient_id,p.first_name,p.last_name,d.specialty
from patients p join admissions a on p.patient_id=a.patient_id
join doctors d on a.attending_doctor_id=d.doctor_id
where a.diagnosis='Epilepsy' and d.first_name='Lisa'
Ques 2 -
Show the percent of patients that have 'M' as their gender.
WITH GenderCount AS (
SELECT
COUNT(*) AS total_patients, -- Count total patients
SUM(CASE WHEN gender = 'M' THEN 1 ELSE 0 END) AS male_patients -- Count male patients
FROM Patients
)
SELECT
(100 * male_patients / total_patients) AS percent_male -- Calculate percentage
FROM GenderCount;
Ques - 3
Show the provinces that has more patients identified as 'M' than 'F'. Must only show full
province_name
SELECT pr.province_name
FROM patients AS pa
JOIN province_names AS pr ON pa.province_id = pr.province_id
GROUP BY pr.province_name
HAVING
COUNT( CASE WHEN gender = 'M' THEN 1 END) > COUNT( CASE WHEN gender = 'F'
THEN 1 END);
Ques - 4
Show all of the patients grouped into weight groups.
Show the total amount of patients in each weight group.
Order the list by the weight group descending.