SQL Syntax Flashcards

1
Q

SELECT and FROM

A

SELECT column 1, column 2
FROM database.table

or

SELECT *
FROM database.table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ORDER BY

A

SELECT *
FROM database.table
ORDER BY column 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

DISTINCT

A

SELECT DISTINCT column 1
FROM database.table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

LIMIT

A

SELECT *
FROM database.table
ORDER BY column 1
LIMIT 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

COUNT

A

SELECT COUNT(*)
FROM database.table

OR
SELECT COUNT(DISTINCT column1)
FROM database.table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

WHERE

A

SELECT *
FROM database.table
WHERE column_1 =10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

AND/OR

A

SELECT *
FROM database.table
WHERE column_1 =10 AND column_2 =11 OR column_3 = 12

OR
SELECT *
FROM database.table
WHERE column_1 =10 AND (column_2 =11 OR column_3 = 12)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

BETWEEN

A

SELECT *
FROM database.table
WHERE column_1 BETWEEN 10 AND 20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

IN

A

SELECT *
FROM database.table
WHERE column_1 IN (1,5,3,7,12)

OR

SELECT *
FROM database.table
WHERE column_1 IN (‘up’,’down’,left’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

LIKE

A

SELECT *
FROM database.table
WHERE column_1 LIKE ‘%text_single character’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

GROUP BY

A

SELECT Column_2, SUM(Column_1)
FROM database.table
GROUP BY Column_2
ORDER BY Column_2

Aggrigates are, Minimum, Maximum, Average, Sum

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

HAVING

A

SELECT Column_2, SUM(Column_1), AVERAGE(Column_1), MIN(Column_1), MAX(Column_1)
FROM database.table
WHERE Column_1 > 0
GROUP BY Column_2
HAVING COUNT(*) > 5
ORDER BY Column_2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

UPPER/LOWER

A

SELECT UPPER(Column_1), LOWER(Column_1), LENGTH(Column_1)
FROM database.table

(returns uppercase, lowercase, length of text.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

LEFT/RIGHT

A

SELECT LEFT(Column_1, 3), RIGHT(Column_1, 3)
FROM database.table

(returns, the left three letters of column_1, returns the right 3 digits of column_1.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

POSITION

A

SELECT POSITION( ‘@’ from email)
FROM database.table

This will return the a number which is the numerical position of the ‘@’ symbol in the text which is an email in this case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

SUBTEXT

A

SELECT SUBTEXT(Column from beginning for lengthofsubtext)

The beginning is where you have to begin in the column, the lengthoftext is which will be returned. If length of text is not entered then the whole end of the text will be returned.

17
Q

EXTRACT

A

SELECT EXTRACT(timeframewanted from column_1)
FROM database.table

18
Q

TO_CHAR

A

SELECT TO_CHAR(Column_date, ‘Day”)
FROM database.table

The ‘Day’ will return a specific day of the column. It can be changed for other commands to return month year etc.

19
Q

CASE

A

SELECT
CASE
WHEN Condition1 THEN result1
WHEN Condition2 THEN result2
WHEN ConditionN THEN resultN
ELSE resultRemaining
END
FROM database.table

20
Q

CONCATINATE

A

SELECT Column_1 || Column_2|| Column_3 AS first_three_columns

this will return the first three columns with no spaces between them as the alias first_three_columns

21
Q

CAST

A

SELECT CAST(ValueOrColumn AS DataType)

The data types may be varying. like DATE, VARCHAR, TEXT , etc. See PostgreSQL Docs

22
Q

REPLACE

A

SELECT REPLACE (Column, ‘Old Text’, ‘New Text’)
FROM database.table

23
Q

INNER JOIN

A

SELECT *
FROM Table_A
INNER JOIN Table_B
ON Table_A.Column_A = Table_B.Column_A

(The On command tells us which are the Primary keys and foreign keys for the table concatenation.)

24
Q

OUTER JOIN

A

SELECT *
FROM table_A
FULL OUTER JOIN table_B
ON table_A.column_1=table_B.column_1

25
Q

LEFT OUTER JOIN

A

SELECT *
FROM table_A
LEFT OUTER JOIN table_B
ON table_A.column_1=table_B.column_1

(FROM is the Primary key, or the main(left) table)

26
Q

RIGHT OUTER JOIN

A

SELECT *
FROM table_A
LEFT OUTER JOIN table_B
ON table_A.column_1=table_B.column_1

(FROM is the Primary key, or the JOINED(RIGHT) table)

27
Q

UNION

A

SELECT * FROM database.table
UNION
SELECT*FROM database.table

(Data types of columns must match, there must be the same number of columns in the correct order of data type.)
(If you do not want duplicates dropped then you will have to use
UNION ALL. other wise duplicates will be dropped.)

28
Q

Correlated SUBQUERY

A

SELECT *
FROM table t1
WHERE column1 = (SELECT AVG(column1) FROM table t2 WHERE t1.column2=t2.column2)