Head First SQL Flashcards

(49 cards)

1
Q

Does this work: WHERE second = “Orange Juice”;

A

No. SQL expects single quote

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

if amount is DEC, does this work: WHERE amount = “1.5”

A

Yes. RDBMS is forgiving

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

Types with quotes

A

CHAR, VARCHAR, DATE, DATETIME, TIME, TIMESTAMP, BLOB

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

Types without quotes

A

DEC INT

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

single quote is a special character

A

' or ‘’

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

Not equal

A

<>

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

Find all the cities ending with CA

A

WHERE location LIKE ‘%CA’. % stands for any number of unknown characters

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

wild card in LIKE which stands for only one character

A

_

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

between two numbers

A

calories BETWEEN 30 AND 60

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

Write a query that will SELECT the names of drinks that begins with letters G through O

A

WHERE drink_name BETWEEN ‘G’ AND ‘O’

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

rating is ‘innovative’ or ‘fabulous‘

A

WHERE rating in (‘innovative’, ‘fabulous’)

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

rating is not ‘innovative’ and ‘fabulous’

A

WHERE rating not in (‘innovative’, ‘fabulous’)

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

Difference:
WHERE NOT main IN ( );
WHERE main NOT IN ( );

A

The same

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

Find IS not NULL

A

WHERE NOT main IS NULL; or

WHERE main IS NOT NULL

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

DELETE records

A

DELECT FROM

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

UPDATE a table

A

UPDATE … SET …

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

Primary key requirement

A
  1. Not NULL 2. can’t be changed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Normal table

A
  1. Atomic 2. Primary key
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Return the CREATE TABLE statement

A

SHOW CREATE TABLE my_contracts;

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

To CREATE TABLE

A

CREATE TABLE my_contract (last_name VARCHAR(20) )

21
Q

AUTO_INCREMENT

A

Automatically fill the column with a value that starts on row 1 with increment of 1 for every new row

22
Q

Add a primary key to an existing table

A

ALTER TABLE XX
ADD COLUMN contact_id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY (contact_id)

23
Q

Renaming the table

A

ALTER TABLE projeckts RENAME TO project_list

24
Q

Remove the primary key designation without changing the data?

A

ALTER TABLE XX DROP PRIMARY KEY;

25
Difference: ADD COLUMN phone VARCHAR(10) LAST; ADD COLUMN phone VARCHAR(10);
Same
26
update values based on different conditions
UPDATE ... SET ... = CASE WHEN ... THEN WHEN... ELSE ...END;
27
Reverse order
ORDER BY ... DESC
28
ORDER
ORDER BY ... ASC
29
mean used with GROUP BY
AVG()
30
select only unique value
SELECT DISTINCT
31
Show only two rows
LIMIT 2;
32
LIMIT 0, 4;
start from 0; show 4 results;
33
parent key
The primary key used by the foreign key
34
Foreign key
a column in a table that reference the primary key of another table
35
Referential Integrity
You will only be able to insert values into your foreign key that exits in the parent table
36
Ways to add foreign key
Creating table | Alter table
37
CONSTRAINT
Make sure the foreign key contains a meaningful value
38
What to add to create a foreign key contact_id when you creating a table? (parent table my_contacts, called contact_id in other table)
contact_id INT NOT NULL CONSTRAINT my_contacts_contact_id_fk FOREIGN KEY (contact_id) REFERENCES my_contacts (contact_id)
39
Composite key
a key made up of more than one column
40
Partial functional dependency
A non-key column is dependent one some, but not all, of the columns in composite primary key
41
Transitive functional dependency
An non-key column is related to another non-key column
42
First normal form (1NF)
Columns contain only atomic values, and no repeating groups of data are permitted in a column
43
Second normal form (2NF)
Your table must be in 1NF and contain no partial functional dependencies to be in 2NF
44
Third normal form (3NF)
Your table must be in 2NF and have no transitive dependencies
45
``` Difference: SELECT profession my_prof FROM my_contacts mc and SELECT profession AS my_prof FROM my_contacts AS mc ```
They're the same
46
CROSS JOIN
Return every row from one table crossed with every row from the second
47
Natural join
Only work if the column you're joining has the same name
48
How many ORDER BY with UNION?
Only one
49
UNION ALL
The same as UNION, except it returns all the values from the columns rather than one instance