-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_PART(1).sql
41 lines (32 loc) · 1.27 KB
/
analysis_PART(1).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
--> Analysis Part(01) : Level Easy
/* Q1: Who is the senior most employee based on job title? */
SELECT employee_id, first_name || ' ' || last_name AS fullname, title
FROM EMPLOYEE
ORDER BY LEVELS DESC
LIMIT 1;
/* Q2: Which countries have the most Invoices? */
SELECT COUNT(*) AS number_of_invoices, billing_country
FROM INVOICE
GROUP BY billing_country
ORDER BY number_of_invoices DESC
LIMIT 1;
/* Q3: What are top 3 values of total invoice ? */
SELECT TOTAL
FROM INVOICE
ORDER BY TOTAL DESC
LIMIT 3;
/* Q4: Which city has the best customers? We would like to throw a promotional Music Festival in the city we made the most money.
Write a query that returns one city that has the highest sum of invoice totals.
Return both the city name & sum of all invoice totals */
SELECT BILLING_CITY, SUM(TOTAL) AS SUM_OF_TOTAL
FROM INVOICE
GROUP BY BILLING_CITY
ORDER BY SUM_OF_TOTAL DESC
LIMIT 1;
/* Q5: Who is the best customer? The customer who has spent the most money will be declared the best customer.
Write a query that returns the person who has spent the most money.*/
SELECT cus.customer_id, first_name, last_name, SUM(inv.total) as total_purchase
FROM CUSTOMER AS cus JOIN INVOICE as inv ON cus.customer_id = inv.customer_id
GROUP BY cus.customer_id
ORDER BY total_purchase DESC
LIMIT 1;