-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.sql
58 lines (49 loc) · 2.21 KB
/
query.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use hospital;
-- 1.Show the employee's first_name and last_name, a "num_orders" column with a count of the orders taken, and a column called "Shipped" that displays "On Time" if the order shipped_date is less or equal to the required_date, "Late" if the order shipped late.
-- Order by employee last_name, then by first_name, and then descending by number of orders
SELECT
e.first_name,
e.last_name,
COUNT(o.order_id) As num_orders,
(
CASE
WHEN o.shipped_date <= o.required_date THEN 'On Time'
ELSE 'Late'
END
) AS shipped
FROM orders o
JOIN employees e ON e.employee_id = o.employee_id
GROUP BY
e.first_name,
e.last_name,
shipped
ORDER BY
e.last_name,
e.first_name,
num_orders DESC
-- Show first name, last name, and gender of patients whose gender is 'M'
SELECT first_name,last_name,gender FROM patients
where gender="M";
--Show how much money the company lost due to giving discounts each year, order the years from most recent to least recent. Round to 2 decimal places
Select
YEAR(o.order_date) AS 'order_year' ,
ROUND(SUM(p.unit_price * od.quantity * od.discount),2) AS 'discount_amount'
from orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
group by YEAR(o.order_date)
order by order_year desc;
--Show unique birth years from patients and order them by ascending.
select distinct year(birth_date) from patients
order by birth_date;
--Show unique first names from the patients table which only occurs once in the list.
--For example, if two or more people are named 'John' in the first_name column then don't include their name in the output list. If only 1 person is named 'Leo' then include them in the output
select first_name from patients
group by first_name
having count(first_name)<=1;
--Show patient_id, attending_doctor_id, and diagnosis for admissions that match one of the two criteria:
--1. patient_id is an odd number and attending_doctor_id is either 1, 5, or 19.
--2. attending_doctor_id contains a 2 and the length of patient_id is 3 characters.
select patient_id,attending_doctor_id,diagnosis from admissions
where (attending_doctor_id in(1,5,19) and patient_id%2!=0)
or (attending_doctor_id like "%2%" and len(patient_id)=3)