SQL Basic PT 2 Flashcards

1
Q

SELECT DISTINCT c1, c2 FROM tbl

A

Select distinct rows by columns c1 and c2 from table tbl

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

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

A

Select 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
3
Q

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

A

Select c1 and use aggregate function on expression expr; group columns by column c1. 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
4
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
5
Q

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

A

Insert data from tbl2 into tbl

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

UPDATE t
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
7
Q

DELETE FROM tbl

WHERE conditions

A

Delete records from table tbl based on WHERE conditions

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

TRUNCATE TABLE tbl

A

Drop table tbl and re-create it, all data is lost

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
CREATE TABLE tbl(
c1 datatype(length)
c2 datatype(length)
...
PRIMARY KEY(c1)
0
A

creates table tbl with primary key as c1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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
11
Q

ALTER TABLE tbl

ADD COLUMN c1 dataype(length)

A

Add column c1 to table tbl

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

ALTER TABLE tbl

DROP COLUMN c1

A

Drop column c1 from table tbl

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

SELECT * FROM tbl1

INNER JOIN tbl 2 ON join-conditions

A

Inner join table tbl1 with tbl2 based on join-conditions

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

SELECT * FROM tbl1

RIGHT JOIN tbl 2 ON join-conditions

A

Right join table tbl1 with tbl2 based on join-conditions

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

The COUNT Function?

A

Returns the number of input rows that match a specific condition of a query

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

SELECT COUNT(DISTINCT title) FROM film;

A

provides the distinct number of titles in film table

17
Q

WHERE statement

A

allows us to select conditions on columns, appears immediately after the FROM clause of the SELECT statement

18
Q
SELECT COUNT(*) FROM payment
WHERE amount NOT IN (0.99, 1.98, 1.99)
A

counts payments that are NOT .99, 1.98 and 1.99

19
Q
SELECT COUNT(*) FROM payment
WHERE amount IN (0.99, 1.98, 1.99)
A

counts payments that are .99, 1.98 and 1.99

20
Q
SELECT COUNT(*) FROM payment
WHERE payment_date BETWEEN '2007-02-01'  AND '2007-02-15'
A

counts payments made between 2/1 and 1/15 (note depending on how inclusive is counted this may count 2007-02-15-00:00 as outside of range

21
Q
SELECT COUNT(*) FROM payment
WHERE amount NOT BETWEEN 8 AND 9
A

count payments NOT between $8 and $9 EXCLUSIVE

22
Q
SELECT COUNT(*) FROM payment
WHERE amount BETWEEN 8 AND 9
A

count payments between $8 and $9 INCLUSIVE

23
Q

%

A

MATCHES ANY SEQUENCE OF CHARACTERS

24
Q

_

A

matches any single character

25
Q

WHERE name LIKE ‘_her%’

A

Returns (Cheryl, Sheryl)

26
Q

SELECT * FROM customer

WHERE first_name LIKE ‘J%’

A

pulls rows from customer where first name starts with ‘J’

27
Q

SELECT * FROM customer

WHERE first_name NOT ILIKE ‘%a%’ AND last_name NOT ILIKE ‘%a%’

A

select all columns from customer tbl where first name doesn’t include any a’s (case-insensitive) and last name doesn’t include any a’s (case-insensitive)