Head First SQL Flashcards

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
Q

Difference:
ADD COLUMN phone VARCHAR(10) LAST;
ADD COLUMN phone VARCHAR(10);

A

Same

26
Q

update values based on different conditions

A

UPDATE … SET … = CASE WHEN … THEN WHEN… ELSE …END;

27
Q

Reverse order

A

ORDER BY … DESC

28
Q

ORDER

A

ORDER BY … ASC

29
Q

mean used with GROUP BY

A

AVG()

30
Q

select only unique value

A

SELECT DISTINCT

31
Q

Show only two rows

A

LIMIT 2;

32
Q

LIMIT 0, 4;

A

start from 0; show 4 results;

33
Q

parent key

A

The primary key used by the foreign key

34
Q

Foreign key

A

a column in a table that reference the primary key of another table

35
Q

Referential Integrity

A

You will only be able to insert values into your foreign key that exits in the parent table

36
Q

Ways to add foreign key

A

Creating table

Alter table

37
Q

CONSTRAINT

A

Make sure the foreign key contains a meaningful value

38
Q

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)

A

contact_id INT NOT NULL
CONSTRAINT my_contacts_contact_id_fk
FOREIGN KEY (contact_id)
REFERENCES my_contacts (contact_id)

39
Q

Composite key

A

a key made up of more than one column

40
Q

Partial functional dependency

A

A non-key column is dependent one some, but not all, of the columns in composite primary key

41
Q

Transitive functional dependency

A

An non-key column is related to another non-key column

42
Q

First normal form (1NF)

A

Columns contain only atomic values, and no repeating groups of data are permitted in a column

43
Q

Second normal form (2NF)

A

Your table must be in 1NF and contain no partial functional dependencies to be in 2NF

44
Q

Third normal form (3NF)

A

Your table must be in 2NF and have no transitive dependencies

45
Q
Difference:
SELECT profession my_prof
FROM my_contacts mc
and
SELECT profession AS my_prof
FROM my_contacts AS mc
A

They’re the same

46
Q

CROSS JOIN

A

Return every row from one table crossed with every row from the second

47
Q

Natural join

A

Only work if the column you’re joining has the same name

48
Q

How many ORDER BY with UNION?

A

Only one

49
Q

UNION ALL

A

The same as UNION, except it returns all the values from the columns rather than one instance