In class SQL commands Pt 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/44

flashcard set

Earn XP

Description and Tags

Week 2

Last updated 1:18 AM on 9/23/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

45 Terms

1
New cards

Select all students

SELECT * FROM students;

2
New cards

Select particular columns

SELECT name, major FROM students;

3
New cards

WHERE with equality

SELECT * FROM students

WHERE major = ‘Computer Science’;

only students whose major is CS is returned

4
New cards

WHERE with numerical comparison

SELECT * FROM students

WHERE student_id > 101;

Only students whose id is greater thna 101 are returned

5
New cards

Other comparison operators

SELECT * FROM students

WHERE student_id <= 102;

6
New cards

WHERE with a character comparison

SELECT * FROM students

WHERE name > ‘Bob’;

SQL compares character strings lexicographically and returns names that sort after Bob

7
New cards

WHERE with IN

SELECT * FROM students

WHERE major IN (‘Computer Science’, ‘Mathematics’);

8
New cards

How does IN select rows?

based on whether a column’s value belongs to a specified set. It does NOT select columns

9
New cards

WHERE with NOT IN

SELECT * FROM students

WHERE major NOT IN (‘Computer Science’);

10
New cards

LIKE with names that begin with A

SELECT * FROM students

WHER name like ‘A%’;

11
New cards

What does the % represent?

represents zero or more characters

12
New cards

LIKE with underscore, return names whose second character is o

SELECT * FROM students

WHERE name LIKE ‘_o%’;

13
New cards

NOT LIKE - return students whose names do not begin with A

SELECT * FROM students

WHERE name NOT LIKE ‘A%’;

14
New cards

Combining conditions with AND. Both must be true, return only cs and IDs > 101

SELECT * FROM students

WHERE major = ‘Computer Science’

AND student_id > 101;

15
New cards

Combining conditions with OR. return student if either condition is true

SELECT * FROM students'

WHERE major = ‘Computer Science’

OR major = ‘Mathematics’;

16
New cards

Combining AND and OR

SELECT * FROM students

WHERE major = ‘Computer Science’

OR (major = ‘Mathematics’ AND student_id > 101);

students satisfying either the first condition or the complete second condition are returned, parentheses make the intended logic explicit

17
New cards

NULL insert a student whose major is unknown

INSERT INTO students VALUES

(105, ‘Eve’, NULL);

18
New cards

Find rows containing NULL

SELECT * FROM students

WHERE major IS NULL;

Eve is returned

19
New cards

What is NULL not the same as?

zero

empty string

or the word ‘NULL’

20
New cards

Find rows that are NOT NULL

SELECT * FROM students

WHERE major IS NOT NULL;

21
New cards

Demonstrate that = NULL does not work

SELECT * FROM students

WHERE major = NULL;

no rows are returned. Since NULL represents an unknown value it cannot be tested using ordinary equality.

22
New cards

Count all enrollment records

SELECT COUNT(*) FROM enrollments;

returns total number of rows in enrollments

23
New cards

Count enrollments by course

SELECT course_id, COUNT(*)

FROM enrollments

GROUP BY course_id;

one group is formed for each course_id and counts the rows in each group

24
New cards

Give the aggregate column an alias

SELECT course_id, COUNT(*) AS enrollment_count

FROM enrollments

GROUP BY course_id;

column named enrollment_count instead of the less descriptive COUNT(*)

25
New cards

Count enrollments by semester

SELECT semester, COUNT(*) AS enrollment_count

FROM enrollments

GROUP BY semester;

all rows having the same semester are placed into one group, and the number of enrollments in each group is counted

26
New cards

What is an aggregate function?

27
New cards

An example with multiple aggregate functions

SELECT course_id,

COUNT(*) AS number_of_students,

MIN(student_id) AS first_student,

MAX(student_id) AS last_student

FROM enrollments

GROUP BY course_id;

28
New cards

Use HAVING to keep groups with multiple students.

How does MySQL first group these?

SELECT course_id, COUNT(*) AS enrollment_count

FROM enrollments

GROUP BY course_id

HAVING COUNT(*) > 1;

MySQL first groups the enrollment records by course, counts the rows in each group, and then keeps only groups whose count is greater than 1

29
New cards

WHERE vs. HAVING

SELECT course_id, COUNT(*) AS enrollment_count

FROM enrollments

WHERE semester = 'Fall 2026'

GROUP BY course_id

HAVING COUNT(*) > 1;

30
New cards

What is the difference between WHERE and HAVING

Key idea: WHERE filters rows BEFORE grouping; HAVING filters groups AFTER grouping

WHERE is applied first and eliminates individual rows whose semester is not Fall 2026. The remaining rows are then grouped by course, and HAVING eliminates groups whose count is not greater than 1.

31
New cards

Showing the difference between WHERE and HAVING EXPLICITLY

SELECT course_id, COUNT(*) AS enrollment_count

FROM enrollments

GROUP BY course_id

HAVING COUNT(*) > 1;


The count is calculated using all rows that reach the grouping stage.

32
New cards

ORDER BY - sort students alphabetically

SELECT * FROM students

ORDER BY name;

sorted by ascending alphabetical order

33
New cards

Sort in descending order

SELECT * FROM students

ORDER BY name DESC;

reverse alphabetical order

34
New cards

Sort by an AGGREGATE value

SELECT course_id, COUNT(*) AS enrollment_count

FROM enrollments

GROUP BY course_id

ORDER BY enrollment_count DESC;

courses re grouped and counted first, and the resulting groups are then sorted from the largest enrollment to the smallest

35
New cards

INSERT one row

INSERT INTO students

VALUES (106, ‘Frank’, ‘Physics’);

36
New cards

Verify the new row

SELECT * FROM students

WHERE student_id = 106;

37
New cards

UPDATE a row

UPDATE students

SET major = ‘Computer Science’

WHERE student_id = 106;

38
New cards

Verify the UPDATE

SELECT * FROM students

WHERE student_id = 106;

39
New cards

UPDATE multiple rows

UPDATE students

SET major = ‘Computer Science’

WHERE major IN (‘Computer Engineering, ‘Computer Science’);

Every row whose major is either CE OR CS is updated

40
New cards

How many rows can an update affect?

An UPDATE can affect MULTIPLE rows when the WHERE condition matches multiple rows

41
New cards

DELETE one row

DELETE FROM students

WHERE student_id = 106;

42
New cards

DELETE using IN

DELETE FROM students

WHERE student_id IN (104,105);

43
New cards

DELETE using multiple conditions

DELETE FROM enrollments

WHERE course_id = ‘CS429’

AND semester = ‘Fall 2026’

AND student_id = 103;

44
New cards

Logical Processing Order

FROM

↓

WHERE ← Eliminate individual rows

↓

GROUP BY ← From Groups

↓

HAVING ← Eliminate groups

↓

SELECT ← Produce the requested columns and expressions

↓

ORDER BY ← sort the results

45
New cards

REMEMBER

• WHERE filters individual rows.

• GROUP BY creates groups of rows.

• Aggregate functions such as COUNT, SUM, AVG, MIN, and MAX operate on groups.

• HAVING filters the resulting groups.

• SELECT determines what appears in the result.

• ORDER BY sorts the final result.