1/68
Looks like no tags are added yet.
What is the purpose of SELECT?
Retrieves specified columns or expressions from one or more tables.
What does FROM do in a SELECT query?
Identifies the source table(s).
What does WHERE do?
Filters individual rows before grouping.
What does GROUP BY do?
Groups rows that have the same values in the specified column(s).
What does HAVING do?
Filters groups after aggregation.
What does ORDER BY do?
Sorts the final result rows.
What does DISTINCT do?
Removes duplicate result rows.
What is the general SELECT query structure?
SELECT [DISTINCT] columns FROM tables [WHERE condition] [GROUP BY columns] [HAVING condition] [ORDER BY columns];
What can appear in the SELECT list of a grouping query?
Aggregate functions and the grouping columns.
Name common SQL aggregate functions.
COUNT, SUM and AVG.
How do you count customers in each country?
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
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.
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation.
Can WHERE contain an aggregate function such as COUNT()?
No. Aggregate conditions belong in HAVING.
What does HAVING often contain?
At least one aggregate function.
How do you show countries with more than five customers?
GROUP BY Country HAVING COUNT(CustomerID)>5
How do you sort customer counts from highest to lowest?
ORDER BY COUNT(CustomerID) DESC
How do you filter countries beginning with U?
WHERE Country LIKE 'U%'
What is a column alias?
An alternative name given to a column or expression in the query result.
How do you create a column alias?
Use AS, e.g. SELECT COUNT(*) AS CustomerCount FROM Customers;
What is a table alias?
A short alternative name used to refer to a table in a query.
How do you create a table alias?
Use AS or omit it, e.g. FROM Customers AS c or FROM Customers c.
Why are table aliases useful?
They shorten table references and distinguish tables, especially in joins and self joins.
What is a subquery?
A SELECT statement nested inside another query.
Where can a subquery appear?
In WHERE, HAVING, SELECT or FROM.
When should = be used with a subquery?
When the inner SELECT returns a single value.
When should IN be used with a subquery?
When the inner SELECT may return one or multiple values.
What does NOT IN do?
Tests whether a value is not among the values returned by a subquery.
What does EXISTS test?
Whether the subquery returns at least one row.
What does NOT EXISTS test?
Whether the subquery returns no rows.
How can you find products supplied by UK suppliers using IN?
WHERE SupplierID IN (SELECT SupplierID FROM Suppliers WHERE Country='UK')
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'.
How can a subquery calculate the average product price?
SELECT AVG(Price) FROM Products
How do you calculate how much a product exceeds the average price?
Price - (SELECT AVG(Price) FROM Products)
What is a JOIN?
An SQL command that combines rows from two or more tables into one result.
Why are JOINs needed in a well-designed database?
Related data is stored in separate tables to reduce duplication; joins query it together.
What is a join condition commonly based on?
Matching a primary key in one table with a foreign key in another.
What is a Cartesian product?
Every row in one table is paired with every row in the other table.
What is an equijoin?
A join whose join condition consists only of equality (=) comparisons.
What is a natural join?
An equijoin on all attributes with the same names, keeping one copy of each common column.
What is an INNER JOIN?
Returns rows that have matching values in both tables.
Can INNER be omitted from INNER JOIN?
Yes. JOIN is equivalent to INNER JOIN.
What is the main risk of NATURAL JOIN?
It automatically joins on every same-named column, so unintended common columns may affect the result.
What is an outer join?
Returns matching row pairs and also preserves unmatched rows from one or both tables.
What does LEFT JOIN return?
All rows from the left table and matching right-table rows; unmatched right-side columns are NULL.
What does RIGHT JOIN return?
All rows from the right table and matching left-table rows; unmatched left-side columns are NULL.
What does FULL JOIN return?
All rows from both tables; missing-side columns are NULL.
Does MySQL directly support FULL OUTER JOIN according to the lecture?
No.
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
What is a self join?
A table joined to itself using different aliases for its different roles.
Why use different aliases in a self join?
To distinguish the two references to the same table.
How can a self join avoid matching a customer with themselves?
Use a condition such as A.CustomerID < B.CustomerID.
How does A.CustomerID < B.CustomerID ensure each pair appears once?
It excludes self-matches and keeps only one ordering of each pair.
What does INSERT do?
Adds new rows to a table.
What is the basic INSERT … VALUES syntax?
INSERT INTO table_name (columns) VALUES (values);
What happens to omitted columns in an INSERT?
They use their defaults, which may be NULL; required columns without defaults must be supplied.
How can INSERT add rows using the result of a query?
INSERT INTO TableName (columnList) SELECT …;
What does UPDATE do?
Modifies existing data in a table.
What is the basic UPDATE syntax?
UPDATE table_name SET column=value WHERE condition;
How do you increase every product price by 3%?
UPDATE Products SET Price=Price*1.03;
What happens if UPDATE omits its WHERE clause?
Every record in the table is updated.
What does DELETE do?
Removes rows of data from a table.
How do you delete all rows from a table?
DELETE FROM table_name;
How do you delete only rows meeting a condition?
DELETE FROM table_name WHERE condition;
What happens if DELETE omits its WHERE clause?
All records in the table are deleted.
How does the lecture represent the current date in MySQL?
CURRENT_DATE()
How does the lecture represent the current date in Oracle?
SYSDATE
What is the key safety warning for UPDATE and DELETE?
Always check the WHERE clause; without it, every row may be changed or deleted.