SQL (Part 2)

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/89

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

90 Terms

1
New cards

Topic / section title

Handling NULL

2
New cards

Handling NULL — what can NULL mean?

NULL can mean: Unknown (not added), Unavailable (can’t be given out), or Not applicable.

3
New cards

NULL and equality (key rule)

NULL can’t be used for equality.

4
New cards

Why NULL can’t be used for equality

Two NULL values may be the same, but we don’t know that.

5
New cards

NULL in WHERE clauses — the question

If NULL appears in a comparison in WHERE, is the result True, False, or something else?

6
New cards

Comparing 2 known (non-NULL) values

Result is clearly TRUE or FALSE.

7
New cards

If NULL is one of the compared values

Depends on meaning: if “not applicable” (and we somehow knew it), answer would be FALSE; if “unknown” or “unavailable”, we can’t say true/false → UNKNOWN.

8
New cards

Three-valued logic name

“Three-value logic” (TRUE, FALSE, UNKNOWN).

9
New cards

What does UNKNOWN generally mean in WHERE?

Generally means “do not select this row”.

10
New cards

UNKNOWN with one condition

Easy: if the condition evaluates to UNKNOWN, don’t select the row.

11
New cards

UNKNOWN with multiple conditions

More complicated with ANDs, ORs, and NOTs (but “common sense” per slides).

12
New cards

Three-valued logic — AND table (overall rule)

AND: either being FALSE means FALSE; both TRUE means TRUE; otherwise UNKNOWN (could go either way).

13
New cards

AND(TRUE, TRUE)

TRUE

14
New cards

AND(TRUE, FALSE)

FALSE

15
New cards

AND(TRUE, UNKNOWN)

UNKNOWN

16
New cards

AND(FALSE, TRUE)

FALSE

17
New cards

AND(FALSE, FALSE)

FALSE

18
New cards

AND(FALSE, UNKNOWN)

FALSE

19
New cards

AND(UNKNOWN, TRUE)

UNKNOWN

20
New cards

AND(UNKNOWN, FALSE)

FALSE

21
New cards

AND(UNKNOWN, UNKNOWN)

UNKNOWN

22
New cards

Three-valued logic — OR table (overall rule)

OR: either being TRUE means TRUE; both FALSE means FALSE; otherwise UNKNOWN (could go either way).

23
New cards

OR(TRUE, TRUE)

TRUE

24
New cards

OR(TRUE, FALSE)

TRUE

25
New cards

OR(TRUE, UNKNOWN)

TRUE

26
New cards

OR(FALSE, TRUE)

TRUE

27
New cards

OR(FALSE, FALSE)

FALSE

28
New cards

OR(FALSE, UNKNOWN)

UNKNOWN

29
New cards

OR(UNKNOWN, TRUE)

TRUE

30
New cards

OR(UNKNOWN, FALSE)

UNKNOWN

31
New cards

OR(UNKNOWN, UNKNOWN)

UNKNOWN

32
New cards

Three-valued logic — NOT table (overall rule)

NOT: UNKNOWN stays UNKNOWN because we don’t know what the opposite is, either.

33
New cards

NOT(TRUE)

FALSE

34
New cards

NOT(FALSE)

TRUE

35
New cards

NOT(UNKNOWN)

UNKNOWN

36
New cards

Checking for NULL in SQL (example query)

SELECT * FROM PERSON WHERE Phone_number IS NOT NULL;

37
New cards

How to test NULL in SQL (important rule)

Use IS and IS NOT for NULL instead of = and <>.

38
New cards

Section / heading

SELECT Math

39
New cards

IN operator purpose

Select a defined set of values.

40
New cards

IN operator works on what types?

Can be numeric; can be strings.

41
New cards

IN operator syntax

… WHERE IN (, , …);

42
New cards

IN operator extra context source (slides)

Extra context from https://www.sqltutorial.org/sql-in/

43
New cards

IN example question

“Find all students who received a C or higher (in any course)”

44
New cards

IN example query

SELECT Studentid, Lettergrade FROM TAKES WHERE Letter_grade IN ('A+','A','A-','B+','B','B-','C+','C');

45
New cards

TAKES table columns shown

Studentid, Courseid, Letter_grade

46
New cards

Aggregation meaning

SQL can do statistics (aggregation functions).

47
New cards

Aggregation functions listed

SUM, MIN/MAX, COUNT, AVG

48
New cards

Where aggregation is done

Aggregation is done in the SELECT clause, not the WHERE.

49
New cards

Why aggregation is in SELECT (slide explanation)

Because the statistics are what we want back!

50
New cards

Aggregation extra context source (slides)

Extra context from https://www.sqltutorial.org/sql-in/

51
New cards

DISTINCT meaning (re-stated)

DISTINCT means “all unique” values.

52
New cards

Opposite of DISTINCT meaning

“All values, including duplicates”.

53
New cards

DISTINCT extra context source (slides)

Extra context from https://www.sqltutorial.org/sql-in/

54
New cards

SUM example question

“Find how much we pay employees per year”

55
New cards

SUM example query

SELECT SUM(Salary) FROM EMPLOYEE;

56
New cards

EMPLOYEE table columns shown in SUM slide

Id, Name, Salary

57
New cards

SUM extra context source (slides)

Extra context from https://www.sqltutorial.org/sql-in/

58
New cards

COUNT example question (departments)

“Find the total number of departments”

59
New cards

COUNT departments example query

SELECT COUNT(DISTINCT Department) FROM EMPLOYEE;

60
New cards

EMPLOYEE table columns shown in COUNT(DISTINCT) slide

Id, Name, Salary, Department

61
New cards

COUNT(DISTINCT …) meaning

Counts distinct (unique) values of the attribute (e.g., unique departments).

62
New cards

COUNT example question (employees)

“Find the total number of employees”

63
New cards

COUNT employees example query

SELECT COUNT(*) FROM EMPLOYEE;

64
New cards

COUNT(*) star note from slides

  • here means “all rows”, not “all attributes”.
65
New cards

COUNT extra context source (slides)

Extra context from https://www.sqltutorial.org/sql-in/

66
New cards

MAX and MIN meaning

“What you’d expect” (max/min values).

67
New cards

MAX example question

“Give me the maximum salary”

68
New cards

MAX example query

SELECT MAX(Salary) FROM EMPLOYEE;

69
New cards

EMPLOYEE table columns shown around MAX slide

Id, Name, Salary, Department

70
New cards

MIN function meaning

Returns the minimum value (paired with MAX as “what you’d expect”).

71
New cards

MAX/MIN extra context source (slides)

Extra context from https://www.sqltutorial.org/sql-in/

72
New cards

AVG meaning

Also what you’d expect (average).

73
New cards

AVG example question

“Give me the average salary”

74
New cards

AVG example query

SELECT AVG(Salary) FROM EMPLOYEE;

75
New cards

EMPLOYEE table columns shown around AVG slide

Id, Name, Salary, Department

76
New cards

AVG extra context source (slides)

Extra context from https://www.sqltutorial.org/sql-in/

77
New cards

Section / heading

DROP

78
New cards

DROP meaning

DROP is the opposite of CREATE (it removes things).

79
New cards

What can you DROP (per slides)?

You can DROP: a table you CREATEd, a domain, or an entire schema (and all the tables inside).

80
New cards

DROP TABLE options

RESTRICT or CASCADE

81
New cards

RESTRICT meaning (DROP TABLE)

Don’t drop the table if it is referenced elsewhere.

82
New cards

RESTRICT example reference

Example: a foreign key refers to this table.

83
New cards

CASCADE meaning (DROP TABLE)

Drop relationships as well.

84
New cards

PostgreSQL CASCADE note

In PostgreSQL (for example), the foreign key constraint will be removed, but the table that had the foreign key will remain.

85
New cards

PostgreSQL dependency reference (slides)

https://www.postgresql.org/docs/current/ddl-depend.html

86
New cards

DROP TABLE effect on data

If your table had data, DROP TABLE will delete that data.

87
New cards

Does DROP TABLE require the table to be empty?

No — the table doesn’t have to be empty.

88
New cards

DROP TABLE missing-table behavior

DROP TABLE will error if the table doesn’t exist.

89
New cards

Avoid error when table might not exist

Use: DROP TABLE IF EXISTS

90
New cards

DROP TABLE IF EXISTS note (slides)

“DROP TABLE IF EXISTS

” avoids the error if the table doesn’t exist.