SQL Commands Flashcards

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
Q

arithmetic operators

A

+, -, *, /

26
Q

display half of age under new column: half-age

A

SELECT age/2 AS ‘half-age’ FROM table_name;

27
Q

display first name and all caps last name

A

SELECT first_name, UPPER(last_name) FROM table_name;

28
Q

functions for adding, square root, and average

A

SUM(), SQRT(), AVG()

29
Q

subquery

A

query within query

30
Q

display if salary is higher than the average salary, sorted from highest to lowest

A

SELECT col_list FROM table_name WHERE salary > (SELECT AVG(salary) FROM table_name) ORDER BY salary DESC;

31
Q

display if name starts with K

A

SELECT col_list FROM table_name WHERE name LIKE ‘K%’;

32
Q

display largest salary in table

A

SELECT MIN(salary) FROM table_name;

33
Q

joining tables…

A

combines data from 2+ to create temp table

34
Q

joining tables means 1 table has

A

reference to col from other table

35
Q

simple join with ID in customers and orders tables sorted by ID from customers table

A

SELECT customers.col_list, orders.col_list FROM customers, orders WHERE customers.ID = orders.ID ORDER BY customers.ID;

36
Q

types of JOIN

A

inner, left, right

37
Q

inner join

A

returns rows when match between tables

38
Q

syntax inner join

A

SELECT col_list FROM table1 INNER JOIN table2 ON table1.col = table2.col;

39
Q

syntax left join

A

SELECT col_list FROM table1 LEFT OUTER JOIN table2 ON table1.col = table2.col;

40
Q

syntax right join

A

SELECT col_list FROM table1 RIGHT OUTER JOIN table2 ON table1.col = table2.col;

41
Q

left join

A

returns all rows from L table even if no matches in R

42
Q

right join

A

returns all rows from R table even if no matches in L

43
Q

union

A

combines mult. datasets into single dataset, removes existing duplicates

44
Q

union all

A

same as union but doesn’t remove duplicate rows, faster

45
Q

in union, all select statements must

A

have same number of cols and same data type and same order

46
Q

UNION syntax

A

SELECT col_list FROM table1 UNION SELECT col_list FROM table2;

47
Q

in union, if cols don’t match exactly across all queries… and ex

A

use NULL: SELECT col_list FROM table1 UNION SELECT col_list NULL FROM table2;

48
Q

add new rows to table in db syntax

A

INSERT INTO table_name VALUES (val1, val2,…);

49
Q

add new employee into employee table: id: 8, name: Anthony Young, age: 35

A

INSERT INTO Employees VALUES (8, ‘Anthony’, ‘Young’, 35);

50
Q

add new employee into employee table: id: 8, name: Anthony Young, age: 35 ; specify col names

A

INSERT INTO Employees (ID, first, last, age) VALUES (8, ‘Anthony’, ‘Young’, 35);