🔍 SELECT Statement
Used to retrieve data from a table.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT first_name, last_name
FROM employees
WHERE department = 'Sales';
✏️UPDATE Statement
Used to modify existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales';
⚠️Always use a WHERE clause unless you want to update all records.
DELETE Statement
Used to remove existing records from a table.
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM employees
WHERE resignation_date IS NOT NULL;
⚠️Be cautious: omitting the WHERE clause deletes all rows in the table.
JOIN 2 TABLES
SELECT O.OrderID, C.CustomerName, O.OrderDate, O.CustomerID
FROM Orders O
INNER JOIN Customers C ON O.CustomerID = C.CustomerID
JOIN 3 tables
SELECT
O.OrderID,
C.CustomerName,
E.EmployeeName,
O.OrderDate
FROM Orders O
JOIN Customers C ON O.CustomerID = C.CustomerID
JOIN Employees E ON O.EmployeeID = E.EmployeeID;
1. Show first name and last name of patients that weight within the range of 100 to 120
(inclusive)
SELECT
first_name,
last_name
FROM patients
WHERE weight BETWEEN 100 AND 120;
2. Show first name, last name, and gender of patients whose gender is 'M'
SELECT
first_name,
last_name,
gender
FROM patients
WHERE gender = 'M';
3. Show first name and last name of patients who does not have allergies. (null)
SELECT
first_name,
last_name
FROM patients
WHERE allergies IS NULL;
4. Show first name of patients that start with the letter 'C'
SELECT
first_name
FROM
patients
WHERE
first_name LIKE 'C%'
5. Show first name, last name, and the full province name of each patient.
Example: 'Ontario' instead of 'ON'
SELECT P.first_name, P.last_name,PR.province_name
FRom PATIENTS P JOIn province_names PR
Where P.province_id = PR.province_id
Or
SELECT
first_name,
last_name,
province_name
FROM patients
JOIN province_names ON province_names.province_id = patients.province_id;
6. Show how many patients have a birth_date with 2010 as the birth year.
SELECT Count(*) From PATIENTS
Where Birth_date like '2010%'
SELECT COUNT(*) AS total_patients
FROM patients
WHERE YEAR(birth_date) = 2009;
7. Show the first_name, last_name, and height of the patient with the greatest height.
SELECT
first_name,
last_name,
height
FROM patients
WHERE height = (
SELECT max(height)
FROM patients
)—subquery
8. Show first name and last name concatinated into one column to show their full name
SELECT
CONCAT(first_name, ' ', last_name) AS full_name
FROM patients;
or
SELECT CONCAT('Miss. ',first_name,' ',last_name)
from Patients
where gender = 'F'
9. Show all columns for patients who have one of the following patient_ids:
1,45,534,879,1000
SELECT *
FROM patients
WHERE
patient_id IN (1, 45, 534, 879, 1000);
or
SELECT * FROM Patients
Where first_name in('Phil','Gala');
OR
SELECT * FROM Patients
Where first_name = 'Phil' OR last_name = 'Littlefield';
10. Show the total number of admissions
Select COUNT(*) AS 'TOTAL ADMISSIONS'
FROM ADMISSIONS
11. Show all the columns from admissions where the patient was admitted and discharged
on the same day.
SELECT * FROM ADMISSIONS
Where admission_date = discharge_date
12. Show the patient id and the total number of admissions for patient_id 579
SELECT
patient_id,
COUNT(*) AS total_admissions
FROM admissions
WHERE patient_id = 579;
13. Based on the cities that our patients live in, show unique cities that are in province_id
'NS'.
SELECT DISTINCT(city) AS unique_cities
FROM patients
WHERE province_id = 'NS';
14. Write a query to find the first_name, last name and birth date of patients who has
height greater than 160 and weight greater than 70
SELECT first_name, last_name, birth_date FROM patients
WHERE height > 160 AND weight > 70;
15. Write a query to find list of patients first_name, last_name, and allergies where
allergies are not null and are from the city of 'Hamilton'
SELECT
first_name,
last_name,
allergies
FROM patients
WHERE
city = 'Hamilton'
and allergies is not null
16. Update the patients table for the allergies column. If the patient's allergies is null then
replace it with 'NKA'
SElect ALLERGIES
FRom patients
Where Allergies is NULL -- precheck
Update Patients SET Allergies ='NKA'
Where ALLERGIES is NULL -- execution
SElect ALLERGIES
FRom patients
Where Allergies is NULL -- postcheck
ROLLBACK -- to rollback the changes
or
COMMIT -- to commit the changes