INNER with 3 tables
SELECT track_id,
-- Enter the correct table name prefix when retrieving the name column from the track table
track.name AS track_name,
title as album_title,
-- Enter the correct table name prefix when retrieving the name column from the artist table
artist.name AS artist_name
FROM track
-- Complete the matching columns to join album with track, and artist with album
INNER JOIN album on track.album_id = album.album_id
INNER JOIN artist on album.artist_id = artist.artist_id;
From an Eurovision festival dataset:
-- Return unique countries and use an alias
SELECT
DISTINCT country AS unique_country
FROM
eurovision;
-- Return all columns, restricting the N percent of rows returned
SELECT
TOP (50) PERCENT *
FROM
eurovision;
From the US power grid outages dataset:
-- Select the top 20 rows from description, nerc_region and event_date
SELECT
TOP (20) description,
nerc_region,
event_date
FROM
grid
-- Order by nerc_region, affected_customers & event_date
-- Event_date should be in descending order
ORDER BY
nerc_region,
affected_customers,
event_date DESC;
-- Select description, affected_customers and event date
SELECT
description,
affected_customers,
event_date
FROM
grid
-- The affected_customers column should be >= 50000 and <=150000
WHERE
affected_customers BETWEEN 50000
AND 150000
-- Define the order
ORDER BY
event_date DESC;
With LIKE, BOOL and parenthesis:
SELECT
artist,
release_year,
song
FROM
songlist
-- Choose the correct artist and specify the release year
WHERE
(
artist LIKE 'B%'
AND release_year = 1986
)
-- Or return all songs released after 1990
OR release_year > 1990
-- Order the results
ORDER BY
release_year,
artist,
song;
Filtering AGGREGATE functions with HAVING
SELECT
country,
COUNT (country) AS country_count,
AVG (place) AS avg_place,
AVG (points) AS avg_points,
MIN (points) AS min_points,
MAX (points) AS max_points
FROM
eurovision
GROUP BY
country
-- The country column should only contain those with a count greater than 5 (aggregate func. with HAVING)
HAVING
COUNT(country) > 5
-- Arrange columns in the correct order
ORDER BY
avg_place,
avg_points DESC;
-- Declare your variables
DECLARE @start DATE
DECLARE @stop DATE
DECLARE @affected INT;
-- SET the relevant values for each variable
SET @start = '2014-01-24'
SET @stop = '2014-07-02'
SET @affected = 5000 ;
SELECT
description,
nerc_region,
demand_loss_mw,
affected_customers
FROM
grid
-- Specify the date range of the event_date and the value for @affected
WHERE event_date BETWEEN @start AND @stop
AND affected_customers >= @affected;
SELECT album.title AS album_title,
artist.name as artist,
MAX(track.milliseconds / (1000 * 60) % 60 ) AS max_track_length_mins
-- Name the temp table #maxtracks
INTO #maxtracks
FROM album
-- Join album to artist using artist_id
INNER JOIN artist ON album.artist_id = artist.artist_id
-- Join track to album using album_id
INNER JOIN track ON track.album_id = album.album_id
GROUP BY artist.artist_id, album.title, artist.name,album.album_id
-- Run the final SELECT query to retrieve the results from the temporary table
SELECT album_title, artist, max_track_length_mins
FROM #maxtracks
ORDER BY max_track_length_mins DESC, artist;
-- Insert unique affiliations into affiliations blank table
INSERT INTO affiliations
SELECT DISTINCT firstname, lastname, function, organization
FROM university_professors;
SELECT transaction_date, amount + CAST(fee AS integer) AS net_amount -- fee was a string in the beginning
FROM transactions;
ALTER TABLE professors
ALTER COLUMN firstname
TYPE varchar(16)
USING SUBSTRING(firstname FROM 1 FOR 16); -- firstname truncated selecting char n.1 to char n.16