SQL Master Document
SQL Master Document
SELECT
description,
event_year,
event_date
FROM
grid;
-- Return 5 rows +-----------------------+
SELECT TOP(5) artist | artist |
FROM artists; |-----------------------|
| AC/DC |
+-------------+ +-------------+
| nerc_region | | nerc_region |
|-------------| |-------------|
| RFC | | NPCC |
| RFC | | NPCC RFC |
| MRO | | RFC |
| MRO | | ERCOT |
| .... | | ... |
+-------------+ +-------------+
-- Return all rows
SELECT *
FROM grid;
SELECT demand_loss_mw AS lost_demand SELECT description AS cause_of_outage
FROM grid; FROM grid;
+-------------+ +------------------------------+
| lost_demand | | cause_of_outage |
|-------------| |------------------------------|
| 424 | | Severe Weather Thunderstorms |
| 217 | | Fuel Supply Emergency Coal |
| 494 | | Physical Attack Vandalism |
| 338 | | Suspected Physical Attack |
| 3900 | | Electrical System Islanding |
| 3300 | +------------------------------+
+-------------+
ORDER BY
SELECT TOP (10) prod_id, year_intro
FROM products
-- Order in ascending order
ORDER BY year_intro, product_id;
+------------+-------------+
| product_id | year_intro |
|------------+-------------|
| 36 | 1981 |
| 37 | 1982 |
| 38 | 1983 |
| 39 | 1984 |
| 40 | 1984 |
| 41 | 1984 |
| 52 | 1985 |
| 43 | 1986 |
| 44 | 1987 |
| 54 | 1987 |
+------------+-------------+
SELECT TOP (10) product_id, year_intro
FROM products
-- Order year_intro in descending order
ORDER BY year_intro DESC, product_id;
+------------+-------------+
| product_id | year_intro |
|------------+-------------|
| 158 | 2015 |
| 173 | 2015 |
| 170 | 2014 |
| 171 | 2014 |
| 172 | 2014 |
| 144 | 2013 |
| 146 | 2013 |
| 147 | 2013 |
| 148 | 2013 |
| 149 | 2013 |
+------------+-------------+
SELECT SELECT
TOP (10) channels, TOP (10) channels,
year_intro year_intro
FROM products FROM products
-- Order in different directions -- Both columns in descending order
ORDER BY ORDER BY
year_intro DESC, year_intro DESC,
channels; channels DESC;
+-------------+------------+ +-------------+------------+
| channels | year_intro | | channels | year_intro |
|-------------+------------| |-------------+------------|
| 35 | 2015 | | 74 | 2015 |
| 74 | 2015 | | 35 | 2015 |
| 29 | 2014 | | 48 | 2014 |
| 45 | 2014 | | 45 | 2014 |
| 48 | 2014 | | 29 | 2014 |
| 12 | 2013 | | 837 | 2013 |
| 13 | 2013 | | 642 | 2013 |
| 14 | 2013 | | 561 | 2013 |
| 22 | 2013 | | 491 | 2013 |
| 24 | 2013 | | 198 | 2013 |
+-------------+------------+ +-------------+------------+
SELECT city_id, name_alias SELECT city_id, name_alias
FROM invoice FROM invoice
-- Ordering text (Ascending order) -- Ordering text (Descending order)
ORDER BY name_alias; ORDER BY name_alias DESC;
+-------------+----------------+ +-------------+----------------+
| city_id | name_alias | | city_id | name_alias |
|-------------+----------------| |-------------+----------------|
| 48 | Amsterdam | | 33 | Yellowknife |
| 59 | Bangalore | | 32 | Winnipeg |
| 36 | Berlin | | 49 | Warsaw |
| 38 | Berlin | | 7 | Vienne |
| 42 | Bordeaux | | 15 | Vancouver |
| 23 | Boston | | 27 | Tucson |
| 13 | Brasília | | 29 | Toronto |
| 8 | Brussels | | 2 | Stuttgart |
| 45 | Budapest | | 51 | Stockholm |
| 56 | Buenos Aires | | 55 | Sydney |
+-------------+----------------+ +-------------+----------------+
SELECT customer_id, total
FROM invoice
WHERE total > 15;
+-------------+------------+
| customer_id | total |
|-------------+------------|
| 57 | 17.91 |
| 7 | 18.86 |
| 45 | 21.86 |
+-------------+------------+
-- Rows with points greater than 10
WHERE points > 10
+------------+-------+
| customerid | total |
|------------+-------|
| 2 | 1.98 |
| 4 | 3.96 |
| 8 | 5.94 |
| 14 | 8.91 |
| 23 | 13.86 |
| 37 | 0.99 |
+------------+-------+
SELECT customer_id, total SELECT customer_id, total
FROM invoice FROM invoice
WHERE total BETWEEN 20 AND 30; WHERE total NOT BETWEEN 20 AND 30;
+------------+--------+ +------------+--------+
| customerid | total | | customerid | total |
|------------+--------| |------------+--------|
| 45 | 21.86 | | 2 | 1.98 |
| 46 | 21.86 | | 4 | 3.96 |
| 26 | 23.86 | | 8 | 5.94 |
| 6 | 25.86 | | 14 | 8.91 |
+------------+--------+ +------------+--------+
SELECT SELECT
TOP (6) total, TOP (6) total,
billing_state billing_state
FROM invoice FROM invoice
WHERE billing_state IS NULL; WHERE billing_state IS NOT NULL;
+-------+---------------+ +--------+---------------+
| total | billing_state | | total | billing_state |
|-------+---------------| |--------+---------------|
| 1.98 | NULL | | 8.91 | AB |
| 3.96 | NULL | | 13.96 | MA |
| 5.94 | NULL | | 5.94 | Dublin |
| 0.99 | NULL | | 0.99 | CA |
| 1.98 | NULL | | 1.98 | WA |
| 1.98 | NULL | | 1.98 | CA |
+-------+---------------+ +-------+----------------+
SELECT song, artist SELECT song, artist
FROM songlist FROM songlist
WHERE WHERE
artist = 'AC/DC'; artist = 'AC/DC'
AND release_year < 1980;
+-------------------------+--------+
| song | artist | +-----------------------------+--------+
|-------------------------+--------| | song | artist |
| Baby, Please Don't Go | AC/DC | |-----------------------------+--------|
| Back In Black | AC/DC | | Dirty Deeds Done Dirt Cheap | AC/DC |
| Big Gun | AC/DC | | Highway To Hell | AC/DC |
| CAN'T STOP ROCK'N'ROLL | AC/DC | | It's A Long Way To The Top | AC/DC |
| Girls Got Rhythm | AC/DC | | Let There Be Rock | AC/DC |
| Hard As A Rock | AC/DC | | Night Prowler | AC/DC |
| Have a Drink On Me | AC/DC | | T.N.T. | AC/DC |
| Hells Bells | AC/DC | | Touch Too Much | AC/DC |
+-------------------------+--------+ | Whole Lotta Rosie | AC/DC |
+-----------------------------+--------+
SELECT * SELECT *
FROM songlist FROM songlist
WHERE WHERE
release_year = 1994 release_year = 1994
AND artist = 'Green Day'; AND artist = 'Green Day'
AND song = 'Basket Case';
SELECT
song,
artist,
release_year
FROM songlist
WHERE release_year = 1994;
+----------------------+---------------------+--------------+
| song | artist | release_year |
|----------------------+---------------------+---------------
| Black Hole Sun | Soundgarden | 1994 |
| Fell On Black Days | Soundgarden | 1994 |
| Spoonman | Soundgarden | 1994 |
| Big Empty | Stone Temple Pilots | 1994 |
| Interstate Love Song | Stone Temple Pilots | 1994 |
| Vasoline | Stone Temple Pilots | 1994 |
+----------------------+---------------------+--------------+
SELECT
song,
artist,
release_year
FROM songlist
WHERE
release_year = 1994
OR release_year > 2000;
+----------------------+---------------------+--------------+
| song | artist | release_year |
|----------------------+---------------------+---------------
| Doom And Gloom | Rolling Stones | 2012 |
| Remedy | Seether | 2005 |
| 45 | Shinedown | 2003 |
| Black Hole Sun | Soundgarden | 1994 |
| Fell On Black Days | Soundgarden | 1994 |
| Spoonman | Soundgarden | 1994 |
| It's Been Awhile | Staind | 2001 |
| Big Empty | Stone Temple Pilots | 1994 |
| Interstate Love Song | Stone Temple Pilots | 1994 |
| Vasoline | Stone Temple Pilots | 1994 |
+----------------------+---------------------+--------------+
SELECT song SELECT song
FROM songlist FROM songlist
WHERE WHERE
artist = 'Green Day' artist = 'Green Day'
AND release_year = 1994; AND release_year > 2000;
+--------------------+ +----------------------------+
| song | | song |
|--------------------| |----------------------------|
| Basket Case | | Boulevard Of Broken Dreams |
| Longview | | Holiday (Live) |
| When I Come Around | | Holiday |
+--------------------+ +----------------------------+
SELECT song +-------------------------------------+
FROM songlist | song |
WHERE |-------------------------------------|
artist = 'Green Day' | Doom And Gloom |
AND release_year = 1994 | Remedy |
OR release_year > 2000; | 45 |
| It's Been Awhile |
| Goodbye Daughters of the Revolution |
| Gold On The Ceiling |
| Lonely Boy |
| Seven Nation Army |
| Get Together |
| Vertigo |
| When I'm Gone |
| ... |
| ... |
+-------------------------------------+
SELECT * SELECT *
FROM songlist FROM songlist
WHERE WHERE
artist = 'Green Day' artist = 'Green Day'
AND release_year = 1994 AND release_year = 1994;
OR release_year > 2000;
SELECT *
FROM songlist
WHERE
release_year > 2000;
SELECT song +--------------------------------------+
FROM songlist | song |
WHERE |--------------------------------------|
artist = 'Green Day' | Basket Case |
AND ( | Boulevard Of Broken Dreams |
release_year = 1994 | Holiday (Live) |
OR release_year > 2000 | Holiday / Boulevard of Broken Dreams |
); | Longview |
| When I Come Around |
+--------------------------------------+
SELECT song
FROM songlist
WHERE
(
artist = 'Green Day'
AND release_year = 1994
)
OR (
artist = 'Green Day'
AND release_year > 2000
);
SELECT song, artist SELECT song, release_year
FROM songlist FROM songlist
WHERE WHERE
artist IN ('Van Halen', 'ZZ Top') release_year IN (1985, 1991, 1992);
ORDER BY song;
+------------------------+--------------+
+----------------------------------+-----------+ | song | release_year |
| song | artist | |------------------------+--------------|
|----------------------------------+-----------| | Addicted to Love | 1985 |
| (Oh) Pretty Woman | Van Halen | | Don't You | 1985 |
| 1984/jump | Van Halen | | Come As You Are | 1991 |
| A Fool for Your Stockings | ZZ Top | | Money for Nothing | 1985 |
| Ain't Talkin' 'bout Love | Van Halen | | Walk of Life | 1985 |
| And the Cradle Will Rock... | Van Halen | | Man On the Moon | 1992 |
| Arrested For Driving While Blind | ZZ Top | | Breaking the Girl | 1992 |
| Atomic Punk | Van Halen | | You Belong to the City | 1985 |
+----------------------------------+-----------+ | Enter Sandman | 1991 |
| In Bloom | 1991 |
+------------------------+--------------+
SELECT song SELECT artist
FROM songlist FROM songlist
WHERE song LIKE 'a%'; WHERE artist LIKE 'f%';
+------------------------+ +---------------+
| song | | artist |
|------------------------| |---------------|
| Addicted to Love | | Faces |
| Ain't Too Proud to Beg | | Faith No More |
+------------------------+ +---------------+
SUM()
SELECT
SUM(affected_customers) AS total_affected
FROM grid;
+----------------+
| total_affected |
|----------------|
| 70143996 |
+----------------+
SELECT
SUM(affected_customers) AS total_affected
FROM grid;
SELECT
SUM (affected_customers) AS total_affected,
SUM (demand_loss_mw) AS total_loss
FROM grid;
+----------------+------------+
| total_affected | total_loss |
|----------------+------------|
| 70143996 | 177888 |
+----------------+------------+
SELECT
SUM (affected_customers) AS total_affected,
(demand_loss_mw) AS total_loss
FROM grid;
+------------------+------------------+ +----------------+------------+
| (No column name) | (No column name) | | total_affected | total_loss |
|------------------+------------------| |----------------+------------|
| 70143996 | 177888 | | 70143996 | 177888 |
+------------------+------------------+ +----------------+------------+
SELECT
COUNT(affected_customers) AS count_affected
FROM grid;
+----------------+
| count_affected |
|----------------|
| 807 |
+----------------+
SELECT
COUNT(DISTINCT affected_customers) AS unique_count_affected
FROM grid;
+-----------------------+
| unique_count_affected |
|-----------------------|
| 280 |
+-----------------------+
SELECT SELECT
MIN(affected_customers) AS min_affected_customers MIN(affected_customers) AS min_affected_customers
FROM grid; FROM grid
WHERE affected_customers > 0;
+------------------------+
| min_affected_customers | +------------------------+
|------------------------| | min_affected_customers |
| 0 | |------------------------|
+------------------------+ | 1 |
+------------------------+
SELECT
MAX(affected_customers) AS max_affected_customers
FROM grid;
+------------------------+
| max_affected_customers |
|------------------------|
| 4645572 |
+------------------------+
SELECT
AVG(affected_customers) AS avg_affected_customers
FROM grid;
+------------------------+
| avg_affected_customers |
|------------------------|
| 86919 |
+------------------------+
SELECT
description,
LEN(description) AS description_length
FROM grid;
+----------------+-----------------------------------+
| description | description_length |
|-------------------------------+--------------------|
| Severe Weather Thunderstorms | 29 |
| Severe Weather Thunderstorms | 29 |
| Severe Weather Thunderstorms | 29 |
| Fuel Supply Emergency Coal | 27 |
| Physical Attack Vandalism | 26 |
+----------------+-----------------------------------+
SELECT
description,
LEFT(description, 20) AS first_20_left
FROM grid;
+-------------------------------+-----------------------+
| description | first_20_left |
|-------------------------------+-----------------------|
| Severe Weather Thunderstorms | Severe Weather Thun |
| Severe Weather Thunderstorms | Severe Weather Thun |
| Severe Weather Thunderstorms | Severe Weather Thun |
| Fuel Supply Emergency Coal | Fuel Supply Emergenc |
| Physical Attack Vandalism | Physical Attack Van |
+-------------------------------+-----------------------+
SELECT
description,
RIGHT(description, 20) AS last_20
FROM grid;
+-------------------------------+----------------------+
| description | last_20 |
|-------------------------------+----------------------|
| Severe Weather Thunderstorms | ather Thunderstorms |
| Severe Weather Thunderstorms | ather Thunderstorms |
| Severe Weather Thunderstorms | ather Thunderstorms |
| Fuel Supply Emergency Coal | pply Emergency Coal |
| Physical Attack Vandalism | al Attack Vandalism |
+-------------------------------+----------------------+
SELECT
CHARINDEX ('_', url) AS char_location,
url
FROM courses;
+---------------+-------------------------------------+
| char_location | url |
|---------------+-------------------------------------|
| 34 | datacamp.com/courses/introduction_ |
| 34 | datacamp.com/courses/intermediate_ |
| 29 | datacamp.com/courses/writing_ |
| 29 | datacamp.com/courses/joining_ |
| 27 | datacamp.com/courses/intro_ |
+---------------+-------------------------------------+
SELECT
SUBSTRING(url, 12, 12) AS target_section,
url
FROM courses;
+------------------+----------------------------------+
| target_section | url |
+------------------+----------------------------------+
| datacamp.com |https//www.datacamp.com/courses |
+------------------+----------------------------------+
SELECT
TOP(5) REPLACE(url, '_', '-') AS replace_with_hyphen
FROM courses;
+-------------------------------------+
| replace_with_hyphen |
+-------------------------------------|
| datacamp.com/courses/introduction- |
| datacamp.com/courses/intermediate- |
| datacamp.com/courses/writing- |
| datacamp.com/courses/joining- |
| datacamp.com/courses/intro- |
+-------------------------------------+
SELECT
SUM(demand_loss_mw) AS lost_demand
FROM grid;
+-------------+
| lost_demand |
+-------------+
| 177888 |
+-------------+
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid;
+----------- -+-------------------------------------------------------+
| lost_demand | description |
+-------------+-------------------------------------------------------+
| NULL | Actual Physical Attack |
| NULL | Cold Weather Event |
| NULL | Cyber Event with Potential to Cause Impact |
| 40 | Distribution Interruption |
| 2 | Distribution System Interruption |
| NULL | Earthquake |
| NULL | Electrical Fault at Generator |
| 338 | Electrical System Islanding |
| 24514 | Electrical System Separation Islanding |
| 15 | Electrical System Separation Islanding Severe Weather |
+-------------+-------------------------------------------------------+
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid
WHERE
description LIKE '%storm'
AND demand_loss_mw IS NOT NULL
GROUP BY description;
+----------- -+--------------------------------------------+
| lost_demand | description |
|-------------+--------------------------------------------|
| 996 | Ice Storm |
| 420 | Load Shed Severe Weather Lightning Storm |
| 332 | Major Storm |
| 3 | Severe Weather Thunderstorm |
| 413 | Severe Weather Wind Storm |
| 4171 | Severe Weather Winter Storm |
| 1352 | Winter Storm |
+-------------+--------------------------------------------+
SELECT
WHERE
GROUP BY
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid
WHERE
description LIKE '%storm'
AND demand_loss_mw IS NOT NULL
GROUP BY description;
+----------- -+--------------------------------------------+
| lost_demand | description |
+-------------+--------------------------------------------+
| 996 | Ice Storm |
| 420 | Load Shed Severe Weather Lightning Storm |
| 332 | Major Storm |
| 3 | Severe Weather Thunderstorm |
| 413 | Severe Weather Wind Storm |
| 4171 | Severe Weather Winter Storm |
| 1352 | Winter Storm |
+-------------+--------------------------------------------+
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid
WHERE
description LIKE '%storm'
AND demand_loss_mw IS NOT NULL
GROUP BY description
HAVING SUM(demand_loss_mw) > 1000;
+----------- -+--------------------------------------------+
| lost_demand | description |
|-------------+--------------------------------------------|
| 4171 | Severe Weather Winter Storm |
| 1352 | Winter Storm |
+-------------+--------------------------------------------+
GROUP BY
WHERE
HAVING GROUP BY
+-----------+-------------------+
| artist_id | name |
|-----------+-------------------|
| 1 | AC/DC |
| 2 | Accept |
| 3 | Aerosmith |
| 4 | Alanis Morissette |
| 5 | Alice In Chains |
+-----------+-------------------+
artist_id
+----------+-------------------------+-----------+
| album_id | title | artist_id |
|----------+-------------------------+-----------|
| 1 | For Those About To Rock | 1 |
| 2 | Balls to the Wall | 2 |
| 3 | Restless and Wild | 2 |
| 4 | Let There Be Rock | 1 |
| 5 | Big Ones | 3 |
+----------+-------------------------+-----------+
album_id
artist_id
artist album
+-----------+-------------------+ +----------+-------------------------+-----------+
| artist_id | name | | album_id | title | artist_id |
|-----------+-------------------| |----------+-------------------------+-----------|
| 1 | AC/DC | | 1 | For Those About To Rock | 1 |
| 2 | Accept | | 2 | Balls to the Wall | 2 |
| 3 | Aerosmith | | 3 | Restless and Wild | 2 |
| 4 | Alanis Morissette | | 4 | Let There Be Rock | 1 |
| 5 | Alice In Chains | | 5 | Big Ones | 3 |
+-----------+-------------------+ +----------+-------------------------+-----------+
artist_id artist
artist album
+-----------+-------------------+ +----------+-------------------------+-----------+
| artist_id | name | | album_id | title | artist_id |
|-----------+-------------------| |----------+-------------------------+-----------|
| 1 | AC/DC | | 1 | For Those About To Rock | 1 |
| 2 | Accept | | 2 | Balls to the Wall | 2 |
| 3 | Aerosmith | | 3 | Restless and Wild | 2 |
| 4 | Alanis Morissette | | 4 | Let There Be Rock | 1 |
| 5 | Alice In Chains | | 5 | Big Ones | 3 |
+-----------+-------------------+ +----------+-------------------------+-----------+
artist_id artist_id
+----------+-------------------------+-----------+-------------+
| album_id | title | artist_id | artist_name |
|----------+---------------------------------------+-----------|
| 1 | For Those About To Rock | 1 | AC/DC |
| 4 | Let There Be Rock | 1 | AC/DC |
+----------+-------------------------+-----------+-------------|
album
artist
artist_id
SELECT
album_id,
title,
album.artist_id,
name AS artist_name
FROM album
INNER JOIN artist ON artist.artist_id = album.artist_id
WHERE album.artist_id = 1;
+----------+-------------------------+-----------+-------------+
| album_id | title | artist_id | artist_name |
|----------+---------------------------------------+-----------|
| 1 | For Those About To Rock | 1 | AC/DC |
| 4 | Let There Be Rock | 1 | AC/DC |
+----------+-------------------------+-----------+-------------|
SELECT
table_A.columnX,
table_A.columnY,
table_B.columnZ
FROM table_A
INNER JOIN table_B ON table_A.foreign_key = table_B.primary_key;
SELECT
album_id,
title,
album.artist_id,
name AS artist_name
FROM album
INNER JOIN artist on artist.artist_id = album.artist_id;
+----------+---------------------------------------+-----------+
| album_id | title | artist_id | artist_name |
|----------+---------------------------------------+-----------|
| 1 | For Those About To Rock | 1 | AC/DC |
| 4 | Let There Be Rock | 1 | AC/DC |
| 2 | Balls To The Wall | 2 | Accept |
| 3 | Restless and Wild | 2 | Accept |
+----------+---------------------------------------+-----------+
album artist
SELECT
table_A.columnX,
table_A.columnY,
table_B.columnZ, table_C columnW
FROM table_A
INNER JOIN table_B ON table_B.foreign_key = table_A.primary_key
INNER JOIN table_C ON table_C.foreign_key = table_B.primary_key;
LEFT RIGHT
LEFT RIGHT
+------------+----------+ +------------+------------+
| Patient_ID | Admitted | | Patient_ID | Discharged |
|------------+----------| |------------+------------|
| 1 | 1 | | 1 | 1 |
| 2 | 1 | | 3 | 1 |
| 3 | 1 | | 4 | 1 |
| 4 | 1 | +------------+------------+
| 5 | 1 |
+------------+----------+
+------------+----------+------------| +------------+----------+------------|
| Patient_ID | Admitted | Discharged | | Patient_ID | Admitted | Discharged |
|------------+----------|------------| |------------+----------|------------|
| 1 | 1 | 1 | | 1 | 1 | 1 |
| 3 | 1 | 1 | | 2 | 1 | NULL |
| 4 | 1 | 1 | | 3 | 1 | 1 |
+------------+----------+------------+ | 4 | 1 | 1 |
| 5 | 1 | NULL |
+------------+----------+------------+
SELECT
Admitted.Patient_ID,
Admitted,
Discharged
FROM Admitted
LEFT JOIN Discharged ON Discharged.Patient_ID = Admitted.Patient_ID;
SELECT
Admitted.Patient_ID,
Admitted,
Discharged
FROM Admitted
LEFT JOIN Discharged ON Discharged.Patient_ID = Admitted.Patient_ID;
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1 | 1 | 1 |
| 2 | 1 | NULL |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 1 | NULL |
+------------+----------+------------+
SELECT
Admitted.Patient_ID,
Admitted,
Discharged
FROM Discharged
RIGHT JOIN Admitted ON Admitted.Patient_ID = Discharged.Patient_ID;
SELECT
Admitted.Patient_ID,
Admitted,
Discharged
FROM Discharged
RIGHT JOIN Admitted ON Admitted.Patient_ID = Discharged.Patient_ID;
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1 | 1 | 1 |
| 2 | 1 | NULL |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 1 | NULL |
+------------+----------+------------+
INNER JOIN
NULL
+------------+-------------------------+------------| +------------+-------------------------+------------|
| album_id | title | artist_id | | album_id | title | artist_id |
|------------+-------------------------|------------| |------------+-------------------------|------------|
| 1 | For Those About To Rock | 1 | | 1 | For Those About To Rock | 1 |
| 4 | Let There Be Rock | 1 | | 4 | Let There Be Rock | 1 |
| 5 | Big Ones | 3 | | 6 | Jagged Little Pill | 4 |
+------------+----------+---------------------------+ | 7 | Facelift | 5 |
+------------+-------------------------+------------+
SELECT +------------+-------------------------+------------|
album_id, | album_id | title | artist_id |
title, |------------+-------------------------|------------|
artist_id | 1 | For Those About To Rock | 1 |
FROM album | 4 | Let There Be Rock | 1 |
WHERE artist_id IN (1, 3) | 5 | Big Ones | 3 |
UNION | 6 | Jagged Little Pill | 4 |
SELECT | 7 | Facelift | 5 |
album_id, +------------+-------------------------+------------+
title,
artist_id
FROM album
WHERE artist_id IN (1, 4, 5);
SELECT +------------+-------------------------+------------|
album_id, | album_id | title | artist_id |
title, |------------+-------------------------|------------|
artist_id | 1 | For Those About To Rock | 1 |
FROM album | 4 | Let There Be Rock | 1 |
WHERE artist_id IN (1, 3) | 5 | Big Ones | 3 |
UNION ALL | 1 | For Those About To Rock | 1 |
SELECT | 4 | Let There Be Rock | 1 |
album_id, | 6 | Jagged Little Pill | 4 |
title, | 7 | Facelift | 5 |
artist_id +------------+-------------------------+------------+
FROM album
WHERE artist_id IN (1, 4, 5);
SELECT +------------+-------------------------+------------|
album_id AS ALBUM_ID, | ALBUM_ID | ALBUM_TITLE | ARTIST_ID |
title AS ALBUM_TITLE, |------------+-------------------------|------------|
artist_id AS ARTIST_ID | 1 | For Those About To Rock | 1 |
FROM album | 4 | Let There Be Rock | 1 |
WHERE artist_id IN(1, 3) | 5 | Big Ones | 3 |
UNION ALL | 1 | For Those About To Rock | 1 |
SELECT | 4 | Let There Be Rock | 1 |
album_id AS ALBUM_ID, | 6 | Jagged Little Pill | 4 |
title AS ALBUM_TITLE, | 7 | Facelift | 5 |
artist_id AS ARTIST_ID +------------+-------------------------+------------+
FROM album
WHERE artist_id IN(1, 4, 5)
UNION UNION ALL
UNION
UNION ALL
SELECT
CREATE TABLE unique table name
SELECT *
UPDATE table
SET column = value
WHERE
-- Condition(s);
WHERE
UPDATE table
SET
column1 = value1,
column2 = value2
WHERE
-- Condition(s);
DELETE
FROM table
WHERE
-- Conditions
SELECT *
FROM artist
WHERE name = 'U2';
DECLARE @
SET @test_int = 5
@my_artist
SELECT --
FROM --
WHERE artist = @my_artist
AND album = @my_album;
SELECT --
FROM --
WHERE artist = @my_artist
AND album = @my_album;
SELECT
col1,
col2,
col3 INTO #my_temp_table
FROM my_existing_table
WHERE
-- Conditions
#my_temp_table
ORDER BY
WHERE HAVING