SQL Statements Flashcards

1
Q

What is the INSERT Statement?

A

May insert a single, or many rows

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

How would you insert a single row?

A

Column names are optional, if column names are not specified, all columns must have values.

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

How would you insert multiple rows?

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

What is the DELETE Statement?

A

May delete a single or many (all) rows. The WHERE clause is optional. If not specified all rows will be deleted without warning.

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

How would you delete a single row?

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

How would you delete multiple rows?

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

How would you delete all rows?

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

What is the UPDATE Statement?

A

May update a single or many(all) rows. Where clause is optional. If not specified all rows will be updated without warning.

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

How would you update a single row?

A

The WHERE clause uses primary key to locate one record

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

How would you update multiple rows?

A

WHERE clause located many records

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

What is the GO Statement?

A

When a “batch” of statements have been executed, the end of the batch can be signified by a GO statement. The GO statement is usually optional, but can be required in certain circumstances. Some CREATE statements require that they are the first statement in a batch.

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

How would you set the date format in an SQL script?

A

SET DATEFORMAT ymd

Sets the date format to YYYY-MM-

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

What is a JOIN?

A

Frequently it is useful to join the data from multiple tables together.

SELECT column(s)

FROM firsttable

JOIN secondtable

ON firsttable.column = secondtable.colum

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

What are the different join types?

A

Inner join (default), left outer join, right outer join, full outer join, cross join.

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

How would you do an inner join?

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

How would you do a left outer join?

A
17
Q

How would you do a right outer join?

A
18
Q

How would you do a full outer join?

A
19
Q

How would you do a cross join?

A
20
Q

How would you join 3 tables?

A
21
Q

How would you join a table with itself?

A
22
Q

What is the proper date format to use?

A

Consider the following date: 01/02/03

Americans would say January 2, 2003

British would say February 1, 2003

Chinese would say February 3, 2001

Canadians wouldn’t be sure

23
Q

What is the DATEDIFF function?

A

SELECT DATEDIFF(DAY, ‘2017-01-01’, ‘2018-09-01’)

Result is 608 days

SELECT DATEDIFF(MONTH, ‘2017-01-01’, ‘2018-09-01’)

Result is 20 months

SELECT DATEDIFF(YEAR, ‘2017-01-01’, ‘2018-09-01’)

Result is 1 year

24
Q

How would you properly someones age with the datediff function?

A

SELECT FLOOR(DATEDIFF(DAY, ‘1999-11-15’, ‘2018-10-02’) / 365.25)

25
Q

What is the DATEADD function?

A

DATEADD(datepart, number, date)

SELECT DATEADD(DAY, 60, ‘2018-09-01’)

Result is 2018-10-31

SELECT DATEADD(MONTH, 5, ‘2018-09-01’)

Result is 2019-02-01

SELECT DATEADD(YEAR, 3, ‘2018-09-01’)

Result is 2021-09-01

26
Q

How would you subtract date instead of add?

A

There is no DATESUB, use DATEADD with negative values

27
Q

What is the GETDATE function?

A

The GETDATE function returns the current date with time

SELECT GETDATE()

To get the date without the time, the CONVERT function can be used

SELECT CONVERT(DATE, GETDATE())

Calculate patient age

SELECT birth_date,

FLOOR(DATEDIFF(DAY, birth_date, GETDATE()) / 365.25)

FROM patients

28
Q

How would you extract the date parts?

A

DAY(date) extracts the day of the month

MONTH(date) extracts the month of the year

YEAR(date) extracts the year

SELECT birth_date,

YEAR(birth_date) AS [year],

MONTH(birth_date) AS [month],

DAY(birth_date) AS [day]

FROM patients

29
Q

What is a Predicate?

A

A predicate specifies a condition that is true, false, or unknown about a given row or group

Used in WHERE clauses

Basic Predicate (=, <>, <, >, <=, >)

BETWEEN Predicate

EXISTS Predicate

IN Predicate

LIKE Predicate

NULL Predicate

30
Q

Why would you use the BETWEEN Predicate?

A

The BETWEEN predicate compares a value inclusively with a range of values more compact version of >= AND <=

eg.

SELECT first_name, last_name, patient_height

FROM patients

WHERE patient_height BETWEEN 100 AND 125

VS

WHERE patient_height >= 100 AND patient_height <= 125

31
Q

How would you use the EXISTS Predicate?

A

The EXISTS predicate tests for the existence of certain rows using a subquery. EXISTS returns true if a subquery contains any rows. eg. Find patients who take at least one medication.

32
Q

What is the IN Predicate?

A

The IN predicate compares a value with a set of values. For many cases, whenever the EXISTS predicate can be used, the IN predicate can be used.

33
Q

How would you use the LIKE predicate?

A
34
Q

How would you use the NULL Predicate?

A
35
Q

How would you use the TOP clause?

A
36
Q

How would you use the TOP PERCENT clause?

A
37
Q

How would you use the HAVING clause?

A
38
Q
A