SQL Fundamentals Lecture Template Flashcards

(55 cards)

1
Q

What is SQL?

A

SQL (Structured Query Language) is used to interact with relational databases, allowing us to retrieve, manipulate, and manage data efficiently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the SELECT statement do?

A

The SELECT statement retrieves data from a database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax for a basic SELECT statement?

A

SELECT column1, column2 FROM table_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you select all columns from a table?

A

SELECT * FROM employees;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the WHERE clause do?

A

The WHERE clause filters data based on a condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Provide an example of filtering data with WHERE.

A

SELECT first_name FROM employees WHERE department = ‘HR’;

Retrieves employees working in the HR department.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does an INNER JOIN do?

A

Returns only matching records from both tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Provide an example of an INNER JOIN.

A

SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;

Retrieves order IDs and corresponding customer names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does a LEFT JOIN do?

A

Returns all records from the left table and matching records from the right.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Provide an example of a LEFT JOIN.

A

SELECT customers.customer_name, orders.order_id FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id;

Shows all customers, even if they haven’t placed orders.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does a RIGHT JOIN do?

A

Returns all records from the right table and matching records from the left.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Provide an example of a RIGHT JOIN.

A

SELECT orders.order_id, customers.customer_name FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does a FULL OUTER JOIN do?

A

Returns all records when there is a match in either table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Provide an example of a FULL OUTER JOIN.

A

SELECT customers.customer_name, orders.order_id FROM customers FULL OUTER JOIN orders ON customers.customer_id = orders.customer_id;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does a CROSS JOIN do?

A

Produces a Cartesian product of two tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Provide an example of a CROSS JOIN.

A

SELECT products.product_name, suppliers.supplier_name FROM products CROSS JOIN suppliers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the UNION operator do?

A

Combines rows from two queries, removing duplicates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Provide an example of the UNION operator.

A

SELECT city FROM customers UNION SELECT city FROM suppliers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does the INTERSECT operator do?

A

Returns only the common rows between two queries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Provide an example of the INTERSECT operator.

A

SELECT city FROM customers INTERSECT SELECT city FROM suppliers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does the MINUS operator do?

A

Returns rows from the first query that are not in the second.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Provide an example of the MINUS operator.

A

SELECT city FROM customers MINUS SELECT city FROM suppliers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does the ORDER BY clause do?

A

Sorts the result set in ascending (ASC) or descending (DESC) order.

24
Q

Provide an example of using ORDER BY.

A

SELECT first_name FROM employees ORDER BY first_name ASC;

25
What does the DISTINCT keyword do?
Removes duplicate values.
26
Provide an example of using DISTINCT.
SELECT DISTINCT department FROM employees;
27
What do LIMIT and OFFSET do?
Limits the number of rows returned.
28
Provide an example of LIMIT and OFFSET.
SELECT * FROM employees LIMIT 5 OFFSET 10; ## Footnote Returns 5 employees, skipping the first 10.
29
What does the LIKE operator do?
Searches for patterns using % (wildcard for multiple characters) and _ (single character).
30
Provide an example of using LIKE.
SELECT first_name FROM employees WHERE first_name LIKE 'J%'; ## Footnote Finds all employees whose first name starts with 'J'.
31
What are aggregate functions?
Functions like AVG, SUM, MAX, MIN, and COUNT that perform calculations on a set of values.
32
Provide an example of using COUNT with GROUP BY.
SELECT department, COUNT(*) FROM employees GROUP BY department; ## Footnote Counts employees per department.
33
What does the HAVING clause do?
Filters grouped results.
34
Provide an example of using HAVING.
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; ## Footnote Returns only departments with more than 5 employees.
35
How do you insert data into a table?
INSERT INTO employees (first_name, last_name, department) VALUES ('John', 'Doe', 'Sales');
36
How do you update data in a table?
UPDATE employees SET department = 'Marketing' WHERE first_name = 'John';
37
How do you delete data from a table?
DELETE FROM employees WHERE department = 'HR';
38
What is the syntax to create a table?
CREATE TABLE employees (id INT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, department VARCHAR(50));
39
How do you alter a table?
ALTER TABLE employees ADD salary INT;
40
How do you drop a table?
DROP TABLE employees;
41
What is a primary key?
Ensures uniqueness for each row.
42
Provide an example of creating a primary key.
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(100));
43
What is a foreign key?
Enforces referential integrity.
44
Provide an example of creating a foreign key.
CREATE TABLE orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(customer_id));
45
What is a unique constraint?
Ensures column values are unique.
46
Provide an example of a unique constraint.
CREATE TABLE users (email VARCHAR(100) UNIQUE);
47
What is a check constraint?
Restricts allowed values.
48
Provide an example of a check constraint.
CREATE TABLE employees (salary INT CHECK (salary > 30000));
49
What is the purpose of indexing?
Speeds up queries.
50
Provide an example of creating an index.
CREATE INDEX idx_name ON employees (first_name, last_name);
51
What is a view in SQL?
A virtual table for querying.
52
Provide an example of creating a view.
CREATE VIEW employee_view AS SELECT first_name, last_name FROM employees;
53
What is a trigger in SQL?
Executes automatically before or after events.
54
Provide an example of creating a trigger.
CREATE TRIGGER before_insert_employee BEFORE INSERT ON employees FOR EACH ROW EXECUTE PROCEDURE check_employee_data();
55
What topics were covered in the lecture?
Querying data, Joins & operators, Sorting & filtering, Aggregate functions, Data manipulation, Managing tables, Constraints, Indexing & views.