๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Database/SQL

[Datacamp] SQL Server

by 5ole 2022. 5. 12.

 

 

[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

๋Œ“๊ธ€