[Datacamp] Data Analyst with SQL Server (Career Track)โ
(1) Introduction to SQL Server ( ๐ )
(2) Introduction to Relational Databases in SQL
(3) Intermediate SQL Server
(4) Time Series Analysis in SQL Server
Selecting : SELECT
Ordering : ORDER BY
Filtering : WHERE, HAVING
Aggregating : SUM, COUNT, MIN, MAX, AVG
Text manipulation : LECT, RIGHT, LEN, SUBSTRING
1. SELECTion Box
- TOP ()
return 5 rows
SELECT TOP(5) artist
FROM artists;
return top 5% of rows
SELECT TOP(5) PERCENT artist
FROM artists;
- DISTINCT
return unique rows
SELECT DISTINCT nerc_region
FROM grid;
- Aliasing column names with AS
SELECT description AS cause_of_outage
FROM grid;
- ORDER BY
order by, where ๋ ๋ค SELECT ๋ฌธ์ ์๋ ์ด ์ฌ์ฉ ๊ฐ๋ฅ
SELECT TOP (10) prod_id, year_intro
FROM products
ORDER BY year_intro, product_id;
DESC
SELECT TOP (10) prod_id, year_intro
FROM products
ORDER BY year_intro DESC, prod_id;
- Non-equality
SELECT customer_id, total
FROM invoice
WHERE total <> 10;
- BETWEEN
SELECT customer_id, total
FROM invoice
WHERE total BETWEEN 20 AND 30;
NOT BETWEEN
SELECT customer_id, total
FROM invoice
WHERE total NOT BETWEEN 20 AND 30;
- IS NULL
SELECT
TOP (6) total,
biling_state
FROM invoice
WHERE billing_state IS NULL;
IS NOT NULL
SELECT
TOP (6) total,
biling_state
FROM invoice
WHERE billing_state IS NOT NULL;
- AND / OR
SELECT song, artist
FROM songlist
WHERE
artist = 'AC/DC'
AND release_year < 1980;
SELECT song, artist
FROM songlist
WHERE
artist = 'Green Day'
AND (
release_year = 1994
OR release_year > 2000;
);
- IN
SELECT song, release_year
FROM songlist
WHERE
release_year IN (1995, 1991, 1992);
- LIKE
SELECT song
FROM songlist
WHERE song LIKE 'a%';
(2) Groups, strings, and counting things
- SUM
Don't forget Aliases, ์ฐ์ง ์์ผ๋ฉด No column name์ผ๋ก ์ถ๋ ฅ๋จ
single column
SELECT
SUM(affected_customers) AS total_affected
FROM grid;
two or more columns
SELECT
SUM(affected_customers) AS total_affected,
SUM(demand_loss_mw) AS total_loss
FROM grid;
- COUNT
SELECT
COUNT(affected_customers) AS count_affected
FROM grid;
distinct
SELECT
SUM(DISTINCT affected_customers) AS unique_count_affected
FROM grid;
- MIN, MAX, AVG
SELECT
MIN(affected_customers) AS min_affected_customers
FROM grid
WHERE affected_customers > 0 ;
- LEN
SELECT
description,
LEN(description) AS description_length
FROM grid;
- LEFT / RIGHT
๋ฌธ์ ์ถ์ถ
SELECT
description,
LEFT(description, 20) AS first_20_left
FROM grid;
- CHARINDEX
ํด๋น ๋ฌธ์ ์์น
SELECT
CHARINDEX('_', url) AS description_length,
url
FROM courses;
- SUBSTRING
(์์ ๋ฌธ์ ์์น, ์ถ์ถํ ๋ฌธ์ ์)
SELECT
SUBSTRING(url, 12, 12) AS target_section,
url
FROM courses;
- REPLACE
SELECT
TOP(5) REPLACE(url, '_', '-') AS replace_with_hyphen
FROM courses;
- GROUP BY
(X) grouping error Syntax => insert GROUP BY
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid;
(O)
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid
GROUP BY description;
- Having
GROUP BY ํ ๊ทธ๋ฃนํ๋ ์ฟผ๋ฆฌ์ ๊ฒฐ๊ณผ๋ฅผ ์ฌํํฐ๋ง (vs. WHERE : ๊ธฐ๋ณธ ํ ๊ฐ์ ์ํฅ์ ์ค)
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid
GROUP BY description
HAVING SUM(demand_loss_mw) > 1000 ;
(3) Joining tables
- INNER JOIN
SELECT
album_id,
title,
album.artist_id,
name AS artist_name
FROM album
INNER JOIN artist ON artist.artist_id = album.artist_id;
Multiple Inner Join
SELECT
track_id,
track.name AS track_name,
title as album_title,
artist.name AS artist_name
FROM track
INNER JOIN album on track.album_id = album.album_id
INNER JOIN artist on album.artist_id = artist.artist_id;
- LEFT Join
no match : NULL ์ถ๋ ฅ
SELECT
Admitted.Patient_ID,
Admitted,
Discharged
FROM Admitted
LEFT JOIN Discharged ON Discharged.Patient_ID = Admitted.Patient_ID;
- RIGHT Join
SELECT
Admitted.Patient_ID,
Admitted,
Discharged
FROM Discharged
RIGHT JOIN Admitted ON Discharged.Patient_ID = Admitted.Patient_ID;
- UNION (Slower to run)
์ค๋ณต ์ ์ธ ๊ฒฐํฉ
๋ค๋ฅธ ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๊ฒฐํฉํ๋ ๊ฒฝ์ฐ ๋์ผํ ์ด ๊ฐฏ์, ์์, ๋ฐ์ดํฐ ์ ํ์ ๊ฐ์ ธ์ผ ํจ
SELECT
album_id,
title,
artist_id
FROM album
WHERE artist_id IN (1, 3)
UNION
SELECT
album_id,
title,
artist_id
FROM album
WHERE artist_id IN (1, 4, 5)
- UNION ALL (Faster to run)
์ค๋ณต ํฌํจ ๋ ์ฟผ๋ฆฌ์ ๋ชจ๋ ํ ๊ฒฐํฉ
๋ค๋ฅธ ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๊ฒฐํฉํ๋ ๊ฒฝ์ฐ ๋์ผํ ์ด ๊ฐฏ์, ์์, ๋ฐ์ดํฐ ์ ํ์ ๊ฐ์ ธ์ผ ํจ
SELECT
album_id,
title,
artist_id
FROM album
WHERE artist_id IN (1, 3)
UNION ALL
SELECT
album_id,
title,
artist_id
FROM album
WHERE artist_id IN (1, 4, 5)
(4) You've got the power
- Data Types
(1) Dates
- date(YYYY-MM-DD), datetime(YYYY-MM-DD hh:mm:ss)
- time
(2) Numeric
- integer, decimal, float
- bit (1=TRUE, 0=FALSE, NULL)
(3) Strings
- char, varchar, nvarchar
- CREATE (column name, data type, size)
CREATE TABLE test_table(
test_date date,
test_name varchar(20),
test_int int
)
- INSERT
INSERT INTO table_name (col1, col2, col3)
VALUES
('value1', 'value2', value3)
- INSERT SELECT
๋ค๋ฅธ ํ ์ด๋ธ์์ ์ถ๊ฐ
Don't use SELECT *
์์๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธ
INSERT INTO table_name (col1, col2, col3)
SELECT
column1,
column2,
column3
FROM other_table
WHERE
- UPDATE
SET ์ ๋ฐ์ดํธํ๋ ค๋ ์ด = ๋ณ๊ฒฝํ๋ ค๋ ๊ฐ
WHERE ์ ๋ฐ์ดํธ ํ ์๋ณ ์กฐ๊ฑด -> WHERE์ ํ์ง ์์ผ๋ฉด ์ด์ ๋ชจ๋ ๊ฐ์ด ์ ๋ฐ์ดํธ ๋จ
UPDATE table
SET
column1 = value1,
column2 = value2
WHERE
- DELETE
ํ์ธ ๋ฉ์์ง๊ฐ ์์ผ๋ ์ฃผ์
DELETE ์ ๋ฏธ๋ฆฌ ํ ์คํธ
DELETE
FROM table
WHERE
- TRUNCATE TABLE
WHERE ์ ํ์์์, ๋ชจ๋ ์ด ๋ชจ๋ ๋ฐ์ดํฐ ํ๋ฒ์ ์ ๊ฑฐ
TRUNCATE TABLE table_name
- Variables
If we change the query, avoid repetition -> create a variable
-- Declare @
DECLARE @my_album_id INT
DECLARE @my_artist VARCHAR(100);
SELECT *
FROM artist
WHERE name = @my_artist;
DECLARE - SET
์ ์ธ - ์ค์
๋์์ ๋ง์กฑ์ํค๋ WHERE์ ์ ์ฐ๊ณ ์ถ์ ๋
DECLARE @my_album_id INT
DECLARE @my_artist VARCHAR(100);
SET @my_album_id = 5
SET @my_artist 'AC/DC';
SELECT --
FROM --
WHERE artist = @my_artist
AND album_id = @my_album_id;
- Temporary tables
#table์ connection, session end ๊น์ง ์กด์ฌํ๋ฉฐ, DROP TABLE๋ก #์๋ ์ ๊ฑฐ ๊ฐ๋ฅ
SELECT
col1,
col2,
col3 INTO #my_temp_table
FROM my_existing_table
WHERE
'Database > SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] LEFT JOIN, UNION ALL, WITH (1) | 2024.01.21 |
---|---|
[MySQL] MySQL ์๋ฒ ๊ตฌ์กฐ์ ํน์ง (0) | 2023.03.27 |
[MySQL] ์ตํฐ๋ง์ด์ (0) | 2023.03.09 |
๋๊ธ