SQL Clauses
Description
SQL clauses are keywords that specify conditions or modify the behavior of SQL statements. They form the building blocks of SQL queries.
Different Clauses
SELECT Clause:
Specifies the columns to be retrieved.
Example:
SELECT name, salary FROM employees;
FROM Clause:
Specifies the table(s) from which to retrieve the data.
Example:
SELECT name, salary FROM employees;
WHERE Clause:
Filters the rows based on a specified condition.
Example:
SELECT name, salary FROM employees WHERE department_id = 10;
GROUP BY Clause:
Groups rows that have the same values in specified columns into aggregated data.
Example:
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;
HAVING Clause:
Filters groups based on a specified condition (used with GROUP BY
).
Example:
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id HAVING AVG(salary) > 50000;
ORDER BY Clause:
Sorts the result set based on one or more columns.
Example:
SELECT name, salary FROM employees ORDER BY salary DESC;
JOIN Clause:
Combines rows from two or more tables based on a related column.
Example:
SELECT employees.name, departments.dept_name
FROM employees
JOIN departments ON employees.dept_id = departments.dept_id;
INSERT INTO Clause:
Adds new rows to a table.
Example:
INSERT INTO employees (name, department_id, salary) VALUES ('John Doe', 10, 60000);
UPDATE Clause:
Modifies existing rows in a table.
Example:
UPDATE employees SET salary = salary + 5000 WHERE department_id = 10;
DELETE Clause:
Removes rows from a table.
Example:
DELETE FROM employees WHERE department_id = 10;
LIMIT/OFFSET Clause:
Specifies the number of rows to return or skip (supported in databases like MySQL, PostgreSQL).
Example:
SELECT * FROM employees LIMIT 10 OFFSET 20;
DISTINCT Clause:
Removes duplicate rows from the result set.
Example:
SELECT DISTINCT department_id FROM employees;
Last updated
Was this helpful?