SQL statements
SQL (Structured Query Language) is a powerful tool for managing and
manipulating relational databases. Below are some common subtopics along
with examples:
1. SELECT Statement:
The SELECT statement is used to retrieve data from one or more database
tables.
SELECT * FROM employees;
2. WHERE Clause:
The WHERE clause is used to filter records based on a specified condition.
SELECT * FROM customers WHERE country = ‘USA’;
3. JOIN:
Joins are used to combine rows from two or more tables based on a related
column.
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
4. GROUP BY:
The GROUP BY clause is used to group rows that have the same values into
summary rows.
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;
5. HAVING Clause:
The HAVING clause is used to filter groups based on a specified condition.
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;
6. ORDER BY:
The ORDER BY clause is used to sort the result set in ascending or
descending order.
SELECT * FROM products
ORDER BY unit_price DESC;
7. Subqueries:
A subquery is a query nested within another query.
SELECT * FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date
> ‘2023-01-01’);
8. Indexes:
Indexes are used to improve the speed of data retrieval operations on
database tables.
CREATE INDEX idx_customer_name ON customers (customer_name);
These are just a few examples of SQL subtopics. Mastering SQL can greatly
enhance your ability to work with relational databases efficiently.
Experimenting with these examples in your own database environment can
help solidify your understanding.