yanganna68 / sql-practice-web-010818

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Intro to SQL

  1. Install the SQLite Browser if you haven't already here
  2. Open the SQLite Browser and click 'File -> Open DataBase'
  3. Choose the chinook.db file from this repo. This database is open source and maintained by Microsoft (SQL is no fun if you don't have any data)

WHAT are the 4 things I can do to data?

Challenges

  1. How would you return all of the rows in the artists table?
SELECT * FROM artists
  1. How would you select the artist with the name "Black Sabbath"
SELECT * FROM artists WHERE name = 'Black Sabbath'
  1. How would you create a table named 'fans' with an autoincrementing ID that's a primary key and a name field of type text
CREATE TABLE fans (
  id INTEGER PRIMARY KEY,
  name TEXT
)
  1. How would you alter the fans table to have a artist_id column type integer?
ALTER TABLE fans
ADD ArtistId INTEGER
  1. How would you add yourself as a fan of the Black Eyed Peas?
INSERT INTO fans (name)
VALUES ('Anna')
  1. How would you return fans that are not fans of the black eyed peas.
SELECT * FROM fans
INNER JOIN artists ON
fans.ArtistId = artists.ArtistId
WHERE
artist.Name <> 'Black Eyed Peas'
  1. Display an artists name next to their album title
  SELECT artists.Name, albums.Title
  FROM artists
  INNER JOIN albums ON
  artists.ArtistId = albums.ArtistId
  1. Display artist name, album name and number of tracks on that album
  SELECT
  artists.Name, albums.Title, COUNT(tracks.TrackId) AS CountTrack
  FROM artists
  INNER JOIN albums ON
  artists.ArtistId = albums.ArtistId
  INNER JOIN tracks ON
  albums.AlbumId = tracks.AlbumId
  GROUP BY artists.Name, albums.Title
  1. How would you return the name of all of the artists in the 'Pop' Genre
SELECT DISTINCT artists.Name
FROM artists
INNER JOIN albums ON
artists.ArtistId = albums.ArtistId
INNER JOIN tracks ON
albums.AlbumId = tracks.AlbumId
INNER JOIN genres ON
tracks.GenreId = genres.GenreId
WHERE genres.Name = 'Pop'
  1. I want to return the names of the artists and their number of rock tracks who play Rock music and have move than 30 tracks in order of the number of rock tracks that they have from greatest to least
SELECT DISTINCT artists.Name, COUNT(tracks.TrackId) AS CountTrack
FROM artists
INNER JOIN albums ON
artists.ArtistId = albums.ArtistId
INNER JOIN tracks ON
albums.AlbumId = tracks.AlbumId
INNER JOIN genres ON
tracks.GenreId = genres.GenreId
WHERE genres.Name = 'Rock'
GROUP BY artists.Name
HAVING CountTrack > 30
ORDER BY CountTrack DESC

About


Languages

Language:Ruby 100.0%