Problem Sets + Solutions for computer science topics
SELECT DISTINCT(YEAR(birth_date))
FROM patients
ORDER BY YEAR(birth_date) ASC;
SELECT DISTINCT(first_name)
FROM patients
GROUP BY first_name
HAVING COUNT(first_name) = 1;
SELECT patient_id, first_name
FROM patients
WHERE first_name LIKE 's____%s';
SELECT patients.patient_id, first_name, last_name
FROM patients JOIN admissions ON patients.patient_id = admissions.patient_id
WHERE diagnosis = 'Dementia';
SELECT first_name
FROM patients
ORDER BY LEN(first_name), first_name;
SELECT
(SELECT COUNT(*) FROM patients WHERE gender = 'M') AS male_count,
(SELECT COUNT(*) FROM patients WHERE gender = 'F') AS female_count;
SELECT first_name, last_name, allergies
FROM patients
WHERE allergies IN ('Penicillin', 'Morphine')
ORDER BY allergies, first_name, last_name;
SELECT patient_id, diagnosis
FROM admissions
GROUP BY patient_id, diagnosis
HAVING COUNT(*) > 1;
SELECT city, COUNT(*) AS city_size
FROM patients
GROUP BY city
ORDER BY city_size DESC, city;
SELECT first_name, last_name, 'Patient' AS role
FROM patients
UNION ALL
SELECT first_name, last_name, 'Doctor' AS role
FROM doctors;
SELECT allergies, COUNT(*) AS allergy_count
FROM patients
GROUP BY allergies
HAVING allergies IS NOT NULL
ORDER BY allergy_count DESC;
SELECT first_name, last_name, birth_date
FROM patients
WHERE YEAR(birth_date) >= 1970 AND year(birth_date) <= 1979
ORDER BY birth_date;
SELECT CONCAT(upper(last_name), ",", LOWER(first_name))
FROM patients
ORDER BY first_name DESC;
SELECT province_id, SUM(height) AS total_height
FROM patients
GROUP BY province_id
HAVING total_height >= 7000;
SELECT MAX(weight) - MIN(weight)
FROM patients
WHERE last_name = 'Maroni';
SELECT DAY(admission_date) AS date, COUNT(*) AS admission_count
FROM admissions
GROUP BY date
ORDER BY admission_count DESC;
SELECT *
FROM admissions
WHERE patient_id = 542
ORDER BY admission_date DESC
LIMIT 1;
SELECT patient_id, attending_doctor_id, diagnosis
FROM admissions
WHERE patient_id % 2 = 1 AND attending_doctor_id IN (1, 5, 19)
OR
attending_doctor_id LIKE '%2%' AND patient_id LIKE '___';
SELECT first_name, last_name, COUNT(*)
FROM admissions JOIN doctors
ON admissions.attending_doctor_id = doctors.doctor_id
GROUP BY doctor_id;
SELECT doctor_id, CONCAT(first_name, " ", last_name), MIN(admission_date), MAX(admission_date)
FROM admissions JOIN doctors
ON admissions.attending_doctor_id = doctors.doctor_id
GROUP BY doctor_id;
SELECT province_name, COUNT(*) AS province_population
FROM patients JOIN province_names
ON patients.province_id = province_names.province_id
GROUP BY province_name
ORDER BY province_population DESC;
SELECT CONCAT(patients.first_name, " ", patients.last_name), admissions.diagnosis, CONCAT(doctors.first_name, " ", doctors.last_name)
FROM admissions
JOIN patients ON patients.patient_id = admissions.patient_id
JOIN doctors ON doctors.doctor_id = admissions.attending_doctor_id;
SELECT first_name, last_name, COUNT(*)
FROM patients
GROUP BY first_name, last_name
HAVING COUNT(*) > 1;
SELECT CONCAT(first_name, " ", last_name) AS full_name,
ROUND(height/30.48, 1) AS height_in_feet,
ROUND(weight * 2.205) AS weight_in_pounds,
birth_date,
CASE
WHEN gender = 'M' THEN 'MALE'
ELSE 'FEMALE'
END AS gender_full
FROM patients;
SELECT patients.patient_id, first_name, last_name
FROM patients
WHERE patients.patient_id NOT IN (SELECT admissions.patient_id FROM admissions);