sql operators clauses
sql operators clauses
SQL (Structured Query Language) uses various clauses to refine, filter, and manipulate data stored in relational databases. These clauses act as directives to structure queries efficiently. Below are the key SQL clauses with syntax and examples.
1. SELECT Clause
The SELECT clause is fundamental in SQL, used to retrieve data from a database. It specifies the columns to be displayed in the output.
Syntax:
SELECT column1, column2, ... FROM table_name;
Example:
Retrieve the names and ages of all employees from the employees table.
SELECT name, age FROM employees;
2. FROM Clause
The FROM clause indicates the table from which data will be fetched. It follows the SELECT clause.
Syntax:
SELECT column1, column2 FROM table_name;
Example:
Retrieve all records from the products table.
SELECT * FROM products;
3. WHERE Clause
The WHERE clause filters records based on specific conditions.
Syntax:
SELECT column1, column2 FROM table_name WHERE condition;
Example:
Fetch employees older than 30.
SELECT name, age FROM employees WHERE age > 30;
4. ORDER BY Clause
The ORDER BY clause arranges results in ascending or descending order.
Syntax:
SELECT column1, column2 FROM table_name ORDER BY column1 ASC|DESC;
Example:
Sort employees by salary in descending order.
SELECT name, salary FROM employees ORDER BY salary DESC;
5. GROUP BY Clause
The GROUP BY clause groups rows with identical values in specified columns. It is often used with aggregate functions.
Syntax:
SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name;
Example:
Find the total sales per product category.
SELECT category, SUM(sales) FROM products GROUP BY category;
6. HAVING Clause
The HAVING clause filters grouped results, unlike WHERE, which filters individual rows.
Syntax:
SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name HAVING condition;
Example:
Show categories where total sales exceed 10,000.
SELECT category, SUM(sales) FROM products GROUP BY category HAVING SUM(sales) > 10000;
7. JOIN Clause
The JOIN clause combines records from multiple tables based on a related column.
Syntax:
SELECT table1.column1, table2.column2 FROM table1 JOIN table2 ON table1.common_column = table2.common_column;
Example:
Retrieve customer orders by joining customers and orders.
SELECT customers.name, orders.order_date FROM customers JOIN orders ON customers.customer_id = orders.customer_id;
8. LIMIT Clause
The LIMIT clause restricts the number of records returned.
Syntax:
SELECT column1, column2 FROM table_name LIMIT number;
Example:
Fetch the first five employees.
SELECT * FROM employees LIMIT 5;
9. DISTINCT Clause
The DISTINCT clause removes duplicate records from the result set.
Syntax:
SELECT DISTINCT column_name FROM table_name;
Example:
Retrieve unique job titles from the employees table.
SELECT DISTINCT job_title FROM employees;
10. UNION Clause
The UNION clause combines results from multiple queries, removing duplicates.
Syntax:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
Example:
Combine customer lists from two regions.
SELECT name FROM customers_region1 UNION SELECT name FROM customers_region2;
Conclusion
SQL clauses optimize data queries by filtering, sorting, grouping, and merging datasets efficiently. Understanding these clauses enhances database management skills, allowing for precise data retrieval and manipulation.