SQL Fundamentals Lecture Template Flashcards
(55 cards)
What is SQL?
SQL (Structured Query Language) is used to interact with relational databases, allowing us to retrieve, manipulate, and manage data efficiently.
What does the SELECT statement do?
The SELECT statement retrieves data from a database.
What is the syntax for a basic SELECT statement?
SELECT column1, column2 FROM table_name;
How do you select all columns from a table?
SELECT * FROM employees;
What does the WHERE clause do?
The WHERE clause filters data based on a condition.
Provide an example of filtering data with WHERE.
SELECT first_name FROM employees WHERE department = ‘HR’;
Retrieves employees working in the HR department.
What does an INNER JOIN do?
Returns only matching records from both tables.
Provide an example of an INNER JOIN.
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.
What does a LEFT JOIN do?
Returns all records from the left table and matching records from the right.
Provide an example of a LEFT JOIN.
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.
What does a RIGHT JOIN do?
Returns all records from the right table and matching records from the left.
Provide an example of a RIGHT JOIN.
SELECT orders.order_id, customers.customer_name FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
What does a FULL OUTER JOIN do?
Returns all records when there is a match in either table.
Provide an example of a FULL OUTER JOIN.
SELECT customers.customer_name, orders.order_id FROM customers FULL OUTER JOIN orders ON customers.customer_id = orders.customer_id;
What does a CROSS JOIN do?
Produces a Cartesian product of two tables.
Provide an example of a CROSS JOIN.
SELECT products.product_name, suppliers.supplier_name FROM products CROSS JOIN suppliers;
What does the UNION operator do?
Combines rows from two queries, removing duplicates.
Provide an example of the UNION operator.
SELECT city FROM customers UNION SELECT city FROM suppliers;
What does the INTERSECT operator do?
Returns only the common rows between two queries.
Provide an example of the INTERSECT operator.
SELECT city FROM customers INTERSECT SELECT city FROM suppliers;
What does the MINUS operator do?
Returns rows from the first query that are not in the second.
Provide an example of the MINUS operator.
SELECT city FROM customers MINUS SELECT city FROM suppliers;
What does the ORDER BY clause do?
Sorts the result set in ascending (ASC) or descending (DESC) order.
Provide an example of using ORDER BY.
SELECT first_name FROM employees ORDER BY first_name ASC;