|
| 1 | +-- easy 1 |
| 2 | + |
| 3 | +-- Q1 : Who is the senior most employee based on job title ? |
| 4 | + |
| 5 | +SELECT * |
| 6 | +FROM employee |
| 7 | +ORDER BY levels DESC |
| 8 | +LIMIT 1 |
| 9 | + |
| 10 | +-- Q2 : Which countries have the most invoices ? |
| 11 | + |
| 12 | +SELECT COUNT(*), billing_country |
| 13 | +FROM invoice |
| 14 | +GROUP BY billing_country |
| 15 | +ORDER BY COUNT(*) DESC |
| 16 | + |
| 17 | +-- Q3 : What are top 3 values of total invoice ? |
| 18 | + |
| 19 | +SELECT total |
| 20 | +FROM invoice |
| 21 | +ORDER BY total DESC |
| 22 | +LIMIT 3 |
| 23 | + |
| 24 | +-- Q4: Which city has the best customers? We would like to throw a |
| 25 | +-- promotional Music Festival in the city we made the most money. Write a |
| 26 | +-- query that returns one city that has the highest sum of invoice totals. |
| 27 | +-- Return both the city name & sum of all invoice totals |
| 28 | + |
| 29 | +SELECT billing_city, SUM(total) as invoice_total |
| 30 | +FROM invoice |
| 31 | +GROUP BY billing_city |
| 32 | +ORDER BY invoice_total DESC |
| 33 | +LIMIT 1 |
| 34 | + |
| 35 | +-- Q5: Who is the best customer? The customer who has spent the most |
| 36 | +-- money will be declared the best customer. Write a query that returns |
| 37 | +-- the person who has spent the most money. |
| 38 | + |
| 39 | +SELECT customer.customer_id,customer.first_name, customer.last_name,SUM(total) AS money |
| 40 | +FROM invoice JOIN customer |
| 41 | +USING (customer_id) |
| 42 | +GROUP BY customer.customer_id |
| 43 | +ORDER BY money DESC |
| 44 | +LIMIT 1 |
| 45 | + |
| 46 | +-- moderate 2 |
| 47 | + |
| 48 | +-- Q1: Write query to return the email, first name, last name, & Genre |
| 49 | +-- of all Rock Music listeners. Return your list ordered alphabetically |
| 50 | +-- by email starting with A |
| 51 | + |
| 52 | +SELECT DISTINCT email, first_name, last_name |
| 53 | +FROM customer c |
| 54 | +JOIN invoice i ON c.customer_id = i.customer_id |
| 55 | +JOIN invoice_line il ON i.invoice_id = il.invoice_id |
| 56 | +WHERE track_id IN ( |
| 57 | + SELECT track_id |
| 58 | + FROM track |
| 59 | + JOIN genre |
| 60 | + ON genre.genre_id = track.genre_id |
| 61 | + WHERE genre.name = 'Rock' |
| 62 | +) |
| 63 | +ORDER BY email |
| 64 | + |
| 65 | +-- Q2 : Let's invite the artists who have written the most rock music in |
| 66 | +-- our dataset. Write a query that returns the Artist name and total |
| 67 | +-- track count of the top 10 rock bands. |
| 68 | + |
| 69 | +SELECT artist.artist_id, artist.name, COUNT(artist.artist_id) AS number_of_songs |
| 70 | +FROM track |
| 71 | +JOIN album ON album.album_id = track.album_id |
| 72 | +JOIN artist ON artist.artist_id = album.artist_id |
| 73 | +JOIN genre ON genre.genre_id = track.genre_id |
| 74 | +WHERE genre.name = 'Rock' |
| 75 | +GROUP BY artist.artist_id |
| 76 | +ORDER BY number_of_songs DESC |
| 77 | +LIMIT 10 |
| 78 | + |
| 79 | + |
| 80 | +-- Q3: Return all the track names that have a song length longer than |
| 81 | +-- the average song length. Return the Name and Milliseconds for |
| 82 | +-- each track. Order by the song length with the longest song listed first. |
| 83 | + |
| 84 | +SELECT name, milliseconds |
| 85 | +FROM track |
| 86 | +WHERE milliseconds > ( |
| 87 | + SELECT AVG(milliseconds) FROM track |
| 88 | +) |
| 89 | +ORDER BY milliseconds DESC |
| 90 | + |
| 91 | +-- advance 3 |
| 92 | + |
| 93 | +-- Q1: Find how much amount spent by each customer on artists? Write a |
| 94 | +-- query to return customer name, artist name and total spent |
| 95 | + |
| 96 | +WITH artists_names AS( |
| 97 | + SELECT ar.artist_id AS artist_id, ar.name AS artist_name, il.invoice_id, SUM(il.unit_price * il.quantity) AS total_spent |
| 98 | + FROM invoice_line il |
| 99 | + JOIN track t ON t.track_id = il.track_id |
| 100 | + JOIN album a ON a.album_id = t.album_id |
| 101 | + JOIN artist ar ON ar.artist_id = a.artist_id |
| 102 | + GROUP BY 1,3 |
| 103 | + ORDER BY 1,3 |
| 104 | +) |
| 105 | + |
| 106 | +SELECT an.artist_name, c.first_name, c.last_name, SUM(an.total_spent) AS total_spent |
| 107 | +FROM customer c |
| 108 | +JOIN invoice i ON c.customer_id = i.customer_id |
| 109 | +JOIN artists_names an ON an.invoice_id = i.invoice_id |
| 110 | +GROUP BY c.customer_id, an.artist_name |
| 111 | +ORDER BY c.customer_id, an.artist_name |
| 112 | + |
| 113 | + |
| 114 | +-- Q2: We want to find out the most popular music Genre for each country. |
| 115 | +-- We determine the most popular genre as the genre with the highest |
| 116 | +-- amount of purchases. Write a query that returns each country along with |
| 117 | +-- the top Genre. For countries where the maximum number of purchases |
| 118 | +-- is shared return all Genres. |
| 119 | + |
| 120 | +WITH all_country AS( |
| 121 | + |
| 122 | + SELECT COUNT(il.quantity) purchases, i.billing_country AS country, g.name AS genre_name, |
| 123 | + ROW_NUMBER() OVER(PARTITION BY i.billing_country ORDER BY COUNT(il.quantity) DESC) AS RowNo |
| 124 | + FROM invoice_line il |
| 125 | + JOIN invoice i ON i.invoice_id = il.invoice_id |
| 126 | + JOIN track t ON t.track_id = il.track_id |
| 127 | + JOIN genre g ON g.genre_id = t.genre_id |
| 128 | + GROUP BY 2,3 |
| 129 | + ORDER BY 2 ASC, 1 DESC |
| 130 | +) |
| 131 | + |
| 132 | +SELECT purchases, country, genre_name FROM all_country WHERE RowNo = 1 |
| 133 | + |
| 134 | + |
| 135 | +-- Q3: Write a query that determines the customer that has spent the most |
| 136 | +-- on music for each country. Write a query that returns the country along |
| 137 | +-- with the top customer and how much they spent. For countries where |
| 138 | +-- the top amount spent is shared, provide all customers who spent this amount |
| 139 | + |
| 140 | +WITH all_row AS( |
| 141 | +SELECT c.customer_id, c.first_name, c.last_name, c.country, SUM(total) AS total_invoice, |
| 142 | +ROW_NUMBER() OVER(PARTITION BY c.country ORDER BY SUM(total) DESC) AS row |
| 143 | +FROM customer c |
| 144 | +JOIN invoice i ON c.customer_id = i.customer_id |
| 145 | +GROUP BY c.customer_id, c.country |
| 146 | +ORDER BY c.customer_id |
| 147 | +) |
| 148 | + |
| 149 | +SELECT * FROM all_row WHERE row = 1 |
0 commit comments