>_FoxiRef
SQL

SQL Cheatsheet

Frequently used SQL commands

#SELECT Queries

CommandDescription
SELECT * FROM tableSelect all columns
SELECT col1, col2 FROM tableSelect specific columns
SELECT DISTINCT col FROM tableSelect distinct values
SELECT col AS alias FROM tableColumn alias
SELECT * FROM table LIMIT 10Limit results
SELECT * FROM table OFFSET 5Skip results

#WHERE Conditions

CommandDescription
WHERE col = valueEquals
WHERE col <> valueNot equals
WHERE col > valueGreater than
WHERE col >= valueGreater than or equal
WHERE col < valueLess than
WHERE col <= valueLess than or equal
WHERE col BETWEEN a AND bBetween range
WHERE col IN (v1, v2, v3)In list
WHERE col LIKE '%pattern%'Pattern matching
WHERE col IS NULLNULL check
WHERE col IS NOT NULLNOT NULL check
WHERE cond1 AND cond2AND condition
WHERE cond1 OR cond2OR condition
WHERE NOT conditionNOT condition

#ORDER BY

CommandDescription
ORDER BY colAscending order
ORDER BY col ASCAscending (explicit)
ORDER BY col DESCDescending order
ORDER BY col1, col2Multiple columns
ORDER BY col NULLS FIRSTNULLs first
ORDER BY col NULLS LASTNULLs last

#Aggregate Functions

CommandDescription
COUNT(*)Count rows
COUNT(col)Count non-null
COUNT(DISTINCT col)Count distinct
SUM(col)Sum
AVG(col)Average
MIN(col)Minimum
MAX(col)Maximum
GROUP BY colGroup by
HAVING conditionGroup filter

#JOINs

CommandDescription
INNER JOIN t2 ON t1.col = t2.colInner join
LEFT JOIN t2 ON t1.col = t2.colLeft outer join
RIGHT JOIN t2 ON t1.col = t2.colRight outer join
FULL OUTER JOIN t2 ON ...Full outer join
CROSS JOIN t2Cross join
SELF JOIN (t AS a, t AS b)Self join
NATURAL JOIN t2Natural join

#INSERT

CommandDescription
INSERT INTO t (cols) VALUES (vals)Insert single row
INSERT INTO t VALUES (v1), (v2)Insert multiple rows
INSERT INTO t SELECT ...Insert from select
INSERT INTO t DEFAULT VALUESInsert default values

#UPDATE

CommandDescription
UPDATE t SET col = valUpdate all rows
UPDATE t SET col = val WHERE ...Conditional update
UPDATE t SET c1 = v1, c2 = v2Update multiple columns
UPDATE t SET col = col + 1Increment value

#DELETE

CommandDescription
DELETE FROM tDelete all rows
DELETE FROM t WHERE ...Conditional delete
TRUNCATE TABLE tTruncate table (fast)

#Table Management

CommandDescription
CREATE TABLE t (col type, ...)Create table
DROP TABLE tDrop table
ALTER TABLE t ADD col typeAdd column
ALTER TABLE t DROP COLUMN colDrop column
ALTER TABLE t RENAME TO newRename table
CREATE INDEX idx ON t(col)Create index
DROP INDEX idxDrop index

#Constraints

CommandDescription
PRIMARY KEYPrimary key
FOREIGN KEY REFERENCES t(col)Foreign key
UNIQUEUnique constraint
NOT NULLNot null
DEFAULT valueDefault value
CHECK (condition)Check constraint
AUTO_INCREMENT / SERIALAuto increment

#Subqueries

CommandDescription
WHERE col IN (SELECT ...)IN subquery
WHERE col = (SELECT ...)Scalar subquery
WHERE EXISTS (SELECT ...)EXISTS subquery
SELECT (SELECT ...) AS colSelect subquery
FROM (SELECT ...) AS tFrom subquery
WITH cte AS (SELECT ...)Common Table Expression

#String/Date Functions

CommandDescription
CONCAT(s1, s2)Concatenate strings
UPPER(str) / LOWER(str)Upper/lower case
LENGTH(str)String length
SUBSTRING(str, start, len)Substring
TRIM(str)Trim whitespace
REPLACE(str, from, to)Replace string
NOW() / CURRENT_TIMESTAMPCurrent timestamp
DATE(datetime)Extract date
YEAR(date) / MONTH(date)Extract year/month
DATEDIFF(d1, d2)Date difference

이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

SQL Cheatsheet - Command Reference | Foxi Dev Reference