Week 7 - databases

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

1/68

encourage image

There's no tags or description

Looks like no tags are added yet.

69 Terms

1
New cards
2
New cards

What is the purpose of SELECT?

Retrieves specified columns or expressions from one or more tables.

3
New cards

What does FROM do in a SELECT query?

Identifies the source table(s).

4
New cards

What does WHERE do?

Filters individual rows before grouping.

5
New cards

What does GROUP BY do?

Groups rows that have the same values in the specified column(s).

6
New cards

What does HAVING do?

Filters groups after aggregation.

7
New cards

What does ORDER BY do?

Sorts the final result rows.

8
New cards

What does DISTINCT do?

Removes duplicate result rows.

9
New cards

What is the general SELECT query structure?

SELECT [DISTINCT] columns FROM tables [WHERE condition] [GROUP BY columns] [HAVING condition] [ORDER BY columns];

10
New cards

What can appear in the SELECT list of a grouping query?

Aggregate functions and the grouping columns.

11
New cards

Name common SQL aggregate functions.

COUNT, SUM and AVG.

12
New cards

How do you count customers in each country?

SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;

13
New cards

Why must City be included in GROUP BY if it appears with Country in SELECT?

A non-aggregate selected column must be a grouping column.

14
New cards

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping; HAVING filters groups after aggregation.

15
New cards

Can WHERE contain an aggregate function such as COUNT()?

No. Aggregate conditions belong in HAVING.

16
New cards

What does HAVING often contain?

At least one aggregate function.

17
New cards

How do you show countries with more than five customers?

GROUP BY Country HAVING COUNT(CustomerID)>5

18
New cards

How do you sort customer counts from highest to lowest?

ORDER BY COUNT(CustomerID) DESC

19
New cards

How do you filter countries beginning with U?

WHERE Country LIKE 'U%'

20
New cards

What is a column alias?

An alternative name given to a column or expression in the query result.

21
New cards

How do you create a column alias?

Use AS, e.g. SELECT COUNT(*) AS CustomerCount FROM Customers;

22
New cards

What is a table alias?

A short alternative name used to refer to a table in a query.

23
New cards

How do you create a table alias?

Use AS or omit it, e.g. FROM Customers AS c or FROM Customers c.

24
New cards

Why are table aliases useful?

They shorten table references and distinguish tables, especially in joins and self joins.

25
New cards

What is a subquery?

A SELECT statement nested inside another query.

26
New cards

Where can a subquery appear?

In WHERE, HAVING, SELECT or FROM.

27
New cards

When should = be used with a subquery?

When the inner SELECT returns a single value.

28
New cards

When should IN be used with a subquery?

When the inner SELECT may return one or multiple values.

29
New cards

What does NOT IN do?

Tests whether a value is not among the values returned by a subquery.

30
New cards

What does EXISTS test?

Whether the subquery returns at least one row.

31
New cards

What does NOT EXISTS test?

Whether the subquery returns no rows.

32
New cards

How can you find products supplied by UK suppliers using IN?

WHERE SupplierID IN (SELECT SupplierID FROM Suppliers WHERE Country='UK')

33
New cards

How can you find products supplied by UK suppliers using EXISTS?

Use EXISTS with a correlated subquery matching p.SupplierID=s.SupplierID and s.Country='UK'.

34
New cards

How can a subquery calculate the average product price?

SELECT AVG(Price) FROM Products

35
New cards

How do you calculate how much a product exceeds the average price?

Price - (SELECT AVG(Price) FROM Products)

36
New cards

What is a JOIN?

An SQL command that combines rows from two or more tables into one result.

37
New cards

Why are JOINs needed in a well-designed database?

Related data is stored in separate tables to reduce duplication; joins query it together.

38
New cards

What is a join condition commonly based on?

Matching a primary key in one table with a foreign key in another.

39
New cards

What is a Cartesian product?

Every row in one table is paired with every row in the other table.

40
New cards

What is an equijoin?

A join whose join condition consists only of equality (=) comparisons.

41
New cards

What is a natural join?

An equijoin on all attributes with the same names, keeping one copy of each common column.

42
New cards

What is an INNER JOIN?

Returns rows that have matching values in both tables.

43
New cards

Can INNER be omitted from INNER JOIN?

Yes. JOIN is equivalent to INNER JOIN.

44
New cards

What is the main risk of NATURAL JOIN?

It automatically joins on every same-named column, so unintended common columns may affect the result.

45
New cards

What is an outer join?

Returns matching row pairs and also preserves unmatched rows from one or both tables.

46
New cards

What does LEFT JOIN return?

All rows from the left table and matching right-table rows; unmatched right-side columns are NULL.

47
New cards

What does RIGHT JOIN return?

All rows from the right table and matching left-table rows; unmatched left-side columns are NULL.

48
New cards

What does FULL JOIN return?

All rows from both tables; missing-side columns are NULL.

49
New cards

Does MySQL directly support FULL OUTER JOIN according to the lecture?

No.

50
New cards

How do you list every customer and their order IDs, including customers with no orders?

FROM Customers C LEFT JOIN Orders O ON C.CustomerID=O.CustomerID

51
New cards

What is a self join?

A table joined to itself using different aliases for its different roles.

52
New cards

Why use different aliases in a self join?

To distinguish the two references to the same table.

53
New cards

How can a self join avoid matching a customer with themselves?

Use a condition such as A.CustomerID < B.CustomerID.

54
New cards

How does A.CustomerID < B.CustomerID ensure each pair appears once?

It excludes self-matches and keeps only one ordering of each pair.

55
New cards

What does INSERT do?

Adds new rows to a table.

56
New cards

What is the basic INSERT … VALUES syntax?

INSERT INTO table_name (columns) VALUES (values);

57
New cards

What happens to omitted columns in an INSERT?

They use their defaults, which may be NULL; required columns without defaults must be supplied.

58
New cards

How can INSERT add rows using the result of a query?

INSERT INTO TableName (columnList) SELECT …;

59
New cards

What does UPDATE do?

Modifies existing data in a table.

60
New cards

What is the basic UPDATE syntax?

UPDATE table_name SET column=value WHERE condition;

61
New cards

How do you increase every product price by 3%?

UPDATE Products SET Price=Price*1.03;

62
New cards

What happens if UPDATE omits its WHERE clause?

Every record in the table is updated.

63
New cards

What does DELETE do?

Removes rows of data from a table.

64
New cards

How do you delete all rows from a table?

DELETE FROM table_name;

65
New cards

How do you delete only rows meeting a condition?

DELETE FROM table_name WHERE condition;

66
New cards

What happens if DELETE omits its WHERE clause?

All records in the table are deleted.

67
New cards

How does the lecture represent the current date in MySQL?

CURRENT_DATE()

68
New cards

How does the lecture represent the current date in Oracle?

SYSDATE

69
New cards

What is the key safety warning for UPDATE and DELETE?

Always check the WHERE clause; without it, every row may be changed or deleted.