Advanced SQL Flashcards

(53 cards)

1
Q

Set

A

each table in a database or the result of a query

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

Empty set

A

query that returns no results

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

Member

A

row within a set

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

3 common set operators

A
  1. Intersection (INTERSECT)
  2. Difference (EXCEPT)
  3. Union
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to join tables?

A

with ON or USING

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

Embedded Select is also called

A

Derived table, cannot use order by, requires an alias

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

What is a correlated sub query?

A

Sub query that references a parent column

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

What types of fields can you join on?

A

Character, number or dates

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

What is a row subquery?

A

embedded select that returns more than 1 column and 0-1 rows

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

What is a table subquery?

A

returns 1 or more columns and 0 or more rows

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

What is a scalar subquery?

A

returns 1 column and 0-1 rows

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

What are the special predicates for sub queries?

A

IN (membership), ALL/ANY/SOME (quantified), EXISTS (existance)

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

How would you use a quantified predicate?

A

WHERE expression (=, , etc) [ANY, ALL, SOME] (subquery)

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

What are the SQL aggregate functions?

A

MIN, MAX, AVG, SUM, COUNT, COUNT distinct

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

Do aggregate functions ignore NULL values?

A

Yes except for COUNT(*)

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

What will an aggregate function return if all values are NULL?

A

NULL

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

T/F: You can embed one aggregate function with another

A

FALSE

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

T/F: you can use an aggregate function in a WHERE clause

A

TRUE, if you use the function in a subquery

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

How do you update a record?

A

UPDATE table_name
SET column_name = value
WHERE condition

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

How do you insert a record?

A

INSERT INTO table_name
(column names)
VALUES (values list)

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

How do you delete a record?

A
DELETE FROM table_name
WHERE exp (where is optional)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is a stored program?

A

1 or more SQL statements stored in DB for later use.

23
Q

What is procedural code?

A

Style of programming that contains procedures/functions completed by software program. Controls the flow of execution

24
Q

What are controlled constructs?

A

Used for selection (if-else, switch, case) and looping (while, do-while, for)

25
What are exception handling?
Managing known error conditions (try, catch blocks)
26
What are SQL control flows?
IF...ELSEIF...ELSE | CASE...WHEN...ELSE
27
What are SQL repeat conditions?
WHILE...DO...LOOP | REPEAT...UNTIL...END REPEAT
28
What is a cursor?
Used to work with data in a result set 1 row at a time (like streaming)
29
What is a handler?
Error handling (condition handler). Syntax is DECLARE [continue or exit] HANDLER FOR [error code]
30
What are the 3 built in named conditions for error codes?
1. NOT FOUND, 2. SQLEXCEPTION, 3. SQLWARNING
31
How do you use outbound parameters?
In sproc, specify OUT param_name. When call procedure, use @var_name to store the out parameter
32
How do you delete a stored procedure?
DROP PROCEDURE [IF EXISTS] proc_name
33
T/F: a function can update, delete or insert data
FALSE
34
What is a trigger?
Named block of code that executes automatically when insert, update, or delete happens. Ex: database logs.
35
T/F: a trigger can be modified
FALSE
36
T/F: a trigger cannot be disabled
TRUE
37
What is an event?
Block of code that runs according to event scheduler. By default event scheduler is off
38
How do you turn the event scheduler on?
SET event_scheduler = [ON or OFF]
39
T/F: turning the event_scheduler on will be permanent
FALSE
40
What is a transaction?
Group of SQL statements that combine into single logical unit of work
41
How do you start a transaction?
START TRANSACTION COMMIT (perm changes DB) OR ROLLBACK (cancels changes)
42
When would you use a transaction?
1. when using 2 or more insert/update/delete statements 2. moving rows from 1 table to another 3. when failure of a change statement would violate data integrity
43
What is a savepoint?
Used to rollback transaction to beginning or at a specific save point. SAVEPOINT name ROLLBACK TO SAVEPOINT name
44
What is concurrency?
When 2 or more users have access to the same database and are trying to update/change data at the same time
45
What are the 4 problems that result from concurrency?
1. Lost updates (changes over write) 2. Dirty Reads (select without latest updates) 3. Non repeatable reads 4. Phantom reads
46
How do you prevent concurrency issues?
Setting/changing the transaction isolation level
47
What is the default transaction isolation level?
REPEATABLE READ (allows inserts, phantom reads can still occur)
48
What concurrency problems are allowed with READ UNCOMMITTED?
All errors allows, limited lock
49
What concurrency problems are allowed with READ COMMITTED?
prevents lost updates, can still get dirty reads, non repeatable or phantom reads
50
What concurrency problems are allowed with REPEATABLE READ?
All problems prevented except phantom reads
51
What concurrency problems are allowed with SERIALIZATION?
all problems are prevented, most secure level
52
What is a deadlock?
Occurs with neither transaction can be committed because each has a lock needed by the other transaction
53
What are the 4 ways to prevent a deadlock?
1. don't allow transactions to stay open long 2. don't use a transaction isolation level higher than necessary 3. make large changes at slow times 4. consider locking when coding transactions