SQL Commands Flashcards

(50 cards)

1
Q

display all records in a table

A

SELECT * FROM table_name;

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

display specified cols in a table

A

SELECT col_list FROM table_name;

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

display no extra copies, only unique values

A

SELECT DISTINCT col_list FROM table_name;

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

keyword to extract only those records w/specified criterion

A

WHERE

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

display data if from the country poland

A

SELECT col_list FROM table_name WHERE country = ‘Poland’;

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

display data if customer id is 2

A

SELECT col_list FROM table_name WHERE customerID = 2;

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

operators

A

, <=, >=, =, != or <>, BETWEEN, LIKE, IN

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

display data if from Berlin, Germany

A

SELECT col_list FROM table_name WHERE country = ‘Germany’ AND city = ‘Berlin’;

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

display data if from Boston, MA or Worcester,MA

A

SELECT col_list FROM table_name WHERE state = ‘MA” AND (city = ‘Boston’ OR city = ‘Worcester’);

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

display info in db and tables

A

SHOW

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

list db’s managed by server

A

SHOW DATABASES

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

display all tables in currently selected db

A

SHOW TABLES

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

display info about cols in given table

A

SHOW COLUMNS … SHOW COLUMNS FROM table_name;

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

display first 5 records from table

A

SELECT col_list FROM table_name LIMIT 5;

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

display 3, 4, 5th records from table

A

SELECT col_list FROM table_name LIMIT 2, 3;

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

typical display with fully qualified name

A

SELECT table_name.col FROM table_name;

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

typical display sorted a->z on col2

A

SELECT col_list FROM table_name ORDER BY col2;

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

typical display sorted z-a by country

A

SELECT col_list FROM table_name ORDER BY country DESC;

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

typical display sorted a-z by name then age

A

SELECT col_list FROM table_name ORDER BY name, age;

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

display data if age is 18-25

A

SELECT col_list FROM table_name WHERE age BETWEEN 18 AND 25;

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

display data if city is included in the following: new york, boston, providence

A

SELECT col_list FROM table_name WHERE city IN (‘New York’, ‘Boston’, ‘Providence’);

22
Q

display data if city is not include the following: la, manchester, douglas

A

SELECT col_list FROM table_name WHERE city NOT IN (‘LA’, ‘Manchester’, ‘Douglas’);

23
Q

display new column that shows first name, city

A

SELECT CONCAT(first_name, ‘, ‘ , city) FROM table_name;

24
Q

display new column that shows first name, city, state displayed under column name: info

A

SELECT CONCAT(first_name, ‘, ‘, city, ‘, ‘ , state) AS ‘info’ FROM table_name; *note: not sure if ‘info’ or info

25
arithmetic operators
+, -, *, /
26
display half of age under new column: half-age
SELECT age/2 AS 'half-age' FROM table_name;
27
display first name and all caps last name
SELECT first_name, UPPER(last_name) FROM table_name;
28
functions for adding, square root, and average
SUM(), SQRT(), AVG()
29
subquery
query within query
30
display if salary is higher than the average salary, sorted from highest to lowest
SELECT col_list FROM table_name WHERE salary > (SELECT AVG(salary) FROM table_name) ORDER BY salary DESC;
31
display if name starts with K
SELECT col_list FROM table_name WHERE name LIKE 'K%';
32
display largest salary in table
SELECT MIN(salary) FROM table_name;
33
joining tables...
combines data from 2+ to create temp table
34
joining tables means 1 table has
reference to col from other table
35
simple join with ID in customers and orders tables sorted by ID from customers table
SELECT customers.col_list, orders.col_list FROM customers, orders WHERE customers.ID = orders.ID ORDER BY customers.ID;
36
types of JOIN
inner, left, right
37
inner join
returns rows when match between tables
38
syntax inner join
SELECT col_list FROM table1 INNER JOIN table2 ON table1.col = table2.col;
39
syntax left join
SELECT col_list FROM table1 LEFT OUTER JOIN table2 ON table1.col = table2.col;
40
syntax right join
SELECT col_list FROM table1 RIGHT OUTER JOIN table2 ON table1.col = table2.col;
41
left join
returns all rows from L table even if no matches in R
42
right join
returns all rows from R table even if no matches in L
43
union
combines mult. datasets into single dataset, removes existing duplicates
44
union all
same as union but doesn't remove duplicate rows, faster
45
in union, all select statements must
have same number of cols and same data type and same order
46
UNION syntax
SELECT col_list FROM table1 UNION SELECT col_list FROM table2;
47
in union, if cols don't match exactly across all queries... and ex
use NULL: SELECT col_list FROM table1 UNION SELECT col_list NULL FROM table2;
48
add new rows to table in db syntax
INSERT INTO table_name VALUES (val1, val2,...);
49
add new employee into employee table: id: 8, name: Anthony Young, age: 35
INSERT INTO Employees VALUES (8, 'Anthony', 'Young', 35);
50
add new employee into employee table: id: 8, name: Anthony Young, age: 35 ; specify col names
INSERT INTO Employees (ID, first, last, age) VALUES (8, 'Anthony', 'Young', 35);