Skip to content

Instantly share code, notes, and snippets.

@byanuaria
Created July 19, 2018 16:06
Show Gist options
  • Save byanuaria/f8e3f5ed394a2838032c8761e035fd5a to your computer and use it in GitHub Desktop.
Save byanuaria/f8e3f5ed394a2838032c8761e035fd5a to your computer and use it in GitHub Desktop.
/* How many albums does the artist Led Zepplin have? */
SELECT COUNT(Albumid) AS totalAlbums
FROM albums
WHERE Artistid = (SELECT Artistid FROM artists WHERE name = 'Led Zeppelin')
/* Create a list of album titles and the unit prices for the artist Audioslave */
SELECT n.Name, u.UnitPrice
FROM ((albums t INNER JOIN artists n
ON t.Artistid = n.Artistid)
INNER JOIN tracks u ON t.Albumid = u.Albumid)
WHERE n.Name = 'Audioslave'
/* Find the first and last name of any customer who does not have
an invoice. Are they any customers returned from the query? */
SELECT n.FirstName, n.LastName, i.Invoiceid
FROM customers n
LEFT JOIN invoices i ON n.Customerid = i.Customerid
WHERE InvoiceId IS NULL
/* Find the total price of each album. What is the total price for
the album Big Ones? */
SELECT t.Title, SUM(p.UnitPrice)
FROM albums t
INNER JOIN tracks p ON t.Albumid = p.Albumid
WHERE t.Title = 'Big Ones'
GROUP BY t.Title
/* How many records are created when you apply a Cartesian
join to the invoice and invoice items table? */
SELECT a.invoice D
FROM invoices a CROSS JOIN invoice_items b;
@vmperrone
Copy link

vmperrone commented Apr 3, 2019

Last one should be:

SELECT a.InvoiceId D FROM invoices a CROSS JOIN invoice_items b;

@Ashish11599
Copy link

In last query there is one mistake
a.invoiceid must be there
I is missing

@monishpatil
Copy link

Last Query can be written as " select count(*) as TotalRecords from Invoices cross join invoice_items "

@Maerushee
Copy link

The last query can be much more effective using :
select count(*)
from invoices CROSS JOIN invoice_items

@avsharma-exe
Copy link

last one can be
SELECT a.invoiceid
FROM invoices a CROSS JOIN invoice_items b;

@DavidCharo
Copy link

last one should be SELECT a.InvoiceId D FROM invoices a CROSS JOIN invoice_items b;

@Shreyasee1999
Copy link

Last one: SELECT inv.invoiceid FROM invoices inv CROSS JOIN invoice_items it;

@Ignaciolandeta
Copy link

alternative last one;

SELECT i.invoiceid
FROM invoices i CROSS JOIN invoice_items i2;

@Vasile11416
Copy link

hey, thanks for uploading this material. how did you get the color for the SQL code, mine is all black and I would like to create a similar page you created. Thank you, V

@Logic2Paradigm
Copy link

Logic2Paradigm commented Jul 26, 2021

First one can be:

SELECT artists.Name
	,albums.ArtistId
	,count(albums.AlbumId) AS sumAlbums
FROM artists
INNER JOIN albums
	ON artists.ArtistId = albums.ArtistId
WHERE artists.Name = 'Led Zeppelin'

@Ignaciolandeta
Copy link

Ignaciolandeta commented Jul 26, 2021 via email

@ismccarthy
Copy link

Here's how I approached the questions for this test. All the answers came out correct. I attempted to keep the statements as readable as possible (for me)

SPOILER ALERT includes answers.

QN 1 - How many albums does the artist Led Zeppelin
have?
-> 14

SELECT COUNT (albumid) AS TotalAlbums
FROM albums, artists
WHERE (artists.artistid = albums.artistid)
AND artists.name = 'Led Zeppelin';

QN 2- Create a list of album titles and the unit prices for the artist "Audioslave".
-> 40 returned

SELECT albums.title, tracks.unitprice
FROM albums, artists, tracks
WHERE (artists.artistid = albums.artistid)
AND albums.albumid = tracks.albumid
AND artists.name = 'Audioslave';

QN 3 - Find the first and last name of any customer who does not have an invoice. Are there any customers returned from the query?
-> none returned

SELECT customers.firstname, customers.lastname
FROM customers, invoices
WHERE customers.customerid
NOT IN (
SELECT invoices.customerid
FROM invoices
INNER JOIN customers
ON customers.customerid = invoices.customerid);

QN 4 - Find the total price for each album.
-> Big Ones = 14.85

SELECT albums.title, SUM(tracks.unitprice) AS Total_Price
FROM albums, tracks
WHERE albums.albumid = tracks.albumid
GROUP BY albums.title;

QN 5 - How many records are created when you apply a Cartesian join to the invoice and invoice items table?
-> 922880

SELECT COUNT(*) AS Total_Records
FROM invoices
CROSS JOIN
invoice_items;

@sachinerokk
Copy link

sachinerokk commented Sep 14, 2021

image

These Queries are from Chinook Database
### Query-1. --How many albums does the artist Led Zeppelin
have?
SELECT artists.Name , COUNT(albums.AlbumID)
FROM artists
INNER JOIN albums
ON artists.ArtistID=albums.ArtistID
WHERE artists.Name='Led Zeppelin'
### Query-2.--Create a list of album titles and the unit prices for the artist "Audioslave".
SELECT albums.Title, tracks.UnitPrice, artists.Name
FROM ((albums LEFT JOIN tracks
ON albums.AlbumID=tracks.AlbumID)
LEFT JOIN artists
ON albums.ArtistID=artists.ArtistID)
WHERE artists.Name='Audioslave'
### Query-3.--Find the first and last name of any customer who
does not have an invoice. Are there any customers returned from the query?
SELECT customers.FirstName, customers.LastName, invoices.CustomerID, invoices.InvoiceID
FROM customers LEFT JOIN invoices
ON customers.CustomerID=invoices.CustomerID
WHERE invoices.InvoiceID IS NULL
### Query-4.--Find the total price for each album.
SELECT albums.AlbumID, albums.Title, SUM(tracks.UnitPrice)
FROM tracks INNER JOIN albums
ON tracks.AlbumID=albums.AlbumID
WHERE albums.Title='Big Ones'
GROUP BY albums.Title
### Query-5.-- How many records are created when you apply a Cartesian join to the invoice and invoice items table?
SELECT a.InvoiceId D
FROM invoices a CROSS JOIN
invoice_items b;

@Mrc1979
Copy link

Mrc1979 commented Sep 22, 2021

SELECT album.Title, sum(track.UnitPrice) as TotalPrice
from album
inner join track
on album.AlbumId = track.AlbumId
group by album.Title
having album.Title = 'Big Ones'

-- => 14.85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment