Commands Flashcards

(62 cards)

1
Q

SELECT * FROM tbl

A

Select all rows and columns from table tbl

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

SELECT c1,c2 FROM tbl

A

Select column c1,c2 and all rows from table tbl

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

SELECT c1,c2 FROM tbl WHERE conditions ORDER BY c1 ASC, c2 DESC

A

Select columns c1, c2 with where conditions and from able tbl. order results by column c1 in ascending order and c2 in descending order

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

SELECT DISTINCT c1,,c2 FROM tbl

A

Select unique rows by columns c1 and c2 from the table

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

SELECT c1, aggregate(expr) FROM tbl GROUP BY c1

A

Select column c1 and use aggregate function on expression expr, group columns by column c1

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

SELECT c1, aggregate(expr) AS c2 FROM tbl GROUP BY c1 HAVING c2 > v

A

Select column c1 and c2 as column alias of the result of aggregate function on expr. Filter group of records with c2 greater than value v

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

INSERT into tbl(c1,c2,…) VALUES(v1,v2…)

A

Insert data into table tbl

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

INSERT INTO tbl(c1,c2,…) SELECT c1,c2.. FROM tbl2 WHERE conditions

A

Insert data from tbl2 into tbl1

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

UPDAT tbl SET c1 = v1 c2 = v2 WHERE conditions

A

Update data in table tbl

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

DELETE FROM tbl WHERE conditions

A

Delete records from table tbl based on conditions

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

TRUNCATE TABLE tbl

A

Drop table tbl and recerate it, losing all data

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

CREATE TABLE tbl( c1 datatype(length) c2 datatype(length)… PRIMARY KEY(c1))

A

create a table with the primary key as c1

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

DROP TABLE tbl

A

Remove table tbl from database

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

ALTER TABLE tbl ADD COLUMN c1 datatype(length)

A

Add a column to the table

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

SELECT column_name FROM table_name

A

Gets you a column from your table

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

SELECT c1,c2 FROM table_1

A

Gets you the specific columns

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

SELECT DISTINCT column FROM table

A

Get the unique values from a column

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

SELECT COUNT(column) FROM table

A

Count all the rows in a column

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

SELECT COUNT( DISTINCT column) FROM TABLE

A

Gets you a number of distinct elements in a table

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

SELECT column FROM table WHERE condition

A

Lets you select a specific set of data

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

Example

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

SELECT column_1, column_2 FROM table ORDER BY column_1 ASC/DESC

A

Order the returned data by a specific column

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

SELECT company,name,sales FROM table ORDER BY company,sales

A

Orders the columns first by all unique companies, then by sales results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
SELECT data FROM table ORDER BY column LIMIT 5
Limits the ordered data to 5 rows
26
SELECT column FROM table BETWEEN low AND high
Gets you data in a range of
27
SELECT value FROM table NOT BETWEEN low AND high
Finds you data within a range of
28
SELECT color FROM table WHERE color IN (‘red’, ‘blue’)
Find rows where the column targetted (color) is in a list of possibilities
29
SELECT name FROM celebrity WHERE name LIKE ‘A%’
Gets all celebrities whose names start with A from a celebrity table
30
SELECT name FROM song WHERE song ILIKE ‘%and’
Gets a list of all songs from a song table that end with and or And
31
WHERE value LIKE ‘value__’
Each underscore is one blank space
32
SELECT ROUND(AVG(num_column),3) FROM TABLE
Returns the average value as a float. Can use ROUND() around the average to get a lower number of decimal places
33
SELECT MAX(num_column) FROM TABLE
Return the maximum
34
SELECT MIN(num_column) FROM table
Return the minimum number of
35
SELECT SUM(num_column) FROM TABLE
Return the sum number
36
SELECT category_col, AGG(data_col) FROM table WHERE condition GROUP BY category_col
It organizes the data into groups. You need to include the columns that you want to group by
37
SELECT company, SUM(sales) FROM finance_table WHERE company != ‘Google’ GROUP BY company HAVING SUM(sales) > 1000
Here having is used to sort companies that have more than 1000 sales. Importantly, having is used when where is filtered by the same thing it’s grouped by
38
AS
Creates an alias for something (table or column) SELECT SUM(column) AS new_name FROM table AS can’t be used in where because it happens at the end of the operation
39
SELECT * FROM TableA INNER JOIN TableB ON TableA.col_match = TableB.col_match
Gets you a table with all the rows where the values of the table A and table B columns match
40
SELECT column, Table2.column, column FROM Table1 INNER JOIN Table2 ON Table1.column = Table2.column
Gets you the columns on either end of the joined column. Also, only gets you one column when joining, not a duplicate
41
JOIN is equal to inner join
There you go
42
SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.col_match = TableB.col_match
Gets everything from both tables. You will get null where there is no match
43
SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.col_match = TableB.col_match WHERE TableA.id IS null OR TableB IS null
Gives you all unique rows from both tables
44
Left outer Join SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.col_match = TableB.col_match
Gets you all of table A and the overlapping columns of table B
45
SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.col_match = TableB.col_match WHERE TableB.id IS null
Gets you only the things unique to table A
46
SELECT * FROM TableA RIGHT OUTER JOIN TableB ON TableA.col_match = TableB.col_match
Gives you all rows where there is overlap in data or only data in Table B
47
SELECT column_name FROM table1 UNION SELECT column_name FROM table2
Combines the columns together to display a new data table
48
TIME
Column with only time
49
DATE
Column with only date
50
TIMESTAMP
Contains sate and time
51
TIMESTAMPTZ
Contains date, time, and timezone
52
TIMEZONE
53
NOW
54
TIMEOFDAY
55
CURRENT_TIME
56
CURRENT_DATE
57
SHOW TIMEZONE
Does what it sounds like
58
SELECT NOW()
Gives an up to the second timestamp
59
SELECT CURRENT_DATE
Gets you the current date
60
EXTRACT(YEAR FROM date_col)
Gets you just the year from a timestamp
61
AGE(date_col)
Gets you an age based on the date in the column
62
TO_CHAR(date_col, ‘mm-dd-yyyy’)
Gets you the year formatted