L.07 Flashcards
Querying in SQL
What SQL command is used to create a table?
CREATE TABLE
What constraint ensures a column has unique values?
UNIQUE
How do you define a primary key in SQL?
PRIMARY KEY(column_name)
What does FOREIGN KEY do?
Ensures referential integrity by linking to another table’s primary key.
What happens if a referenced primary key is deleted in a foreign key relationship?
Actions can be CASCADE, SET NULL, SET DEFAULT, or NO ACTION.
What is the purpose of the INSERT statement?
To add new rows into a table.
How do you retrieve all columns from a table?
SELECT * FROM table_name;
How do you rename an attribute in SQL?
Using AS, e.g., SELECT column_name AS alias FROM table_name;
What SQL clause is used to filter query results?
WHERE
How do you select only unique values from a table?
Use SELECT DISTINCT column_name FROM table_name;
What arithmetic operations can be used in SQL queries?
+, -, *, /, %
How do you filter results based on multiple conditions?
Use logical operators AND, OR, NOT
What SQL operator checks if a value is within a range?
BETWEEN
How do you check for NULL values?
IS NULL or IS NOT NULL
How do you check if a value is in a specific set of values?
IN (value1, value2, …)
What clause is used to sort query results?
ORDER BY
How do you perform a basic SQL join?
SELECT columns FROM table1 INNER JOIN table2 ON table1.key = table2.key;
What are the different types of SQL joins?
INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN
What is a self-join?
A join where a table is joined with itself using table aliases.
What is a natural join?
A join that automatically matches columns with the same name.
What set operators does SQL support?
UNION, INTERSECT, EXCEPT
What are subqueries used for?
To execute a nested query within another query.
What are aggregate functions?
COUNT, SUM, AVG, MAX, MIN
How do you group data in SQL?
Using GROUP BY