SQL Examples
SQL Examples
FROM table_name
WHERE condition;
Example:
FROM employees
✏️UPDATE Statement
UPDATE table_name
WHERE condition;
Example:
UPDATE employees
⚠️Always use a WHERE clause unless you want to update all records.
DELETE Statement
WHERE condition;
Example:
⚠️Be cautious: omitting the WHERE clause deletes all rows in the table.
JOIN 2 TABLES
FROM Orders O
JOIN 3 tables
SELECT
O.OrderID,
C.CustomerName,
E.EmployeeName,
O.OrderDate
FROM Orders O
SELECT
first_name,
last_name
FROM patients
2. Show first name, last name, and gender of patients whose gender is 'M'
SELECT
first_name,
last_name,
gender
FROM patients
3. Show first name and last name of patients who does not have allergies. (null)
SELECT
first_name,
last_name
FROM patients
4. Show first name of patients that start with the letter 'C'
SELECT
first_name
FROM
patients
WHERE
Or
SELECT
first_name,
last_name,
province_name
FROM patients
6. Show how many patients have a birth_date with 2010 as the birth year.
FROM patients
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
FROM patients;
or
from Patients
9. Show all columns for patients who have one of the following patient_ids:
1,45,534,879,1000
SELECT *
FROM patients
WHERE
or
OR
FROM ADMISSIONS
11. Show all the columns from admissions where the patient was admitted and discharged
on the same day.
12. Show the patient id and the total number of admissions for patient_id 579
SELECT
patient_id,
COUNT(*) AS total_admissions
FROM admissions
13. Based on the cities that our patients live in, show unique cities that are in province_id
'NS'.
FROM patients
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,
allergies
FROM patients
WHERE
city = 'Hamilton'
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
SElect ALLERGIES
FRom patients
or