SQL Queries
SQL Queries
Now, let's list the possible SELECT queries along with their notes, syntax, examples, and outputs
for the employees table:
1. Basic SELECT
Note: Retrieve data from a database.
Syntax: SELECT column1, column2, ... FROM table_name;
Example:
Sql
Output:
2. SELECT ALL
Note: Retrieve all columns.
Syntax: SELECT * FROM table_name;
Example:
Sql
Output:
3. SELECT DISTINCT
Note: Retrieve unique values.
Syntax: SELECT DISTINCT column1 FROM table_name;
Example:
Sql
Output:
4. WHERE Clause
Note: Filter records.
Syntax: SELECT column1, column2, ... FROM table_name WHERE condition;
Example:
Sql
Output:
5. ORDER BY Clause
Note: Sort results.
Syntax: SELECT column1, column2, ... FROM table_name ORDER BY column1 ASC|DESC;
Example:
Sql
Output:
6. LIMIT Clause
Note: Limit the number of records.
Syntax: SELECT column1, column2, ... FROM table_name LIMIT number;
Example:
Sql
Output:
7. JOINs
Note: Combine rows from two or more tables.
Inner Join Syntax: SELECT columns FROM table1 INNER JOIN table2 ON table1.common_
column = table2.common_column;
Example:
Sql
Output: This example assumes the presence of another table departments with depart
ment_id and department_name columns.
8. GROUP BY Clause
Note: Group rows that have the same values.
Syntax: SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
Example:
Sql
Output:
9. HAVING Clause
Note: Filter groups.
Syntax: SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING conditi
on;
Example:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Output:
13. MAX Function
Note: Find the maximum value.
Syntax: SELECT MAX(column) FROM table_name;
Example:
Sql
Output:
Sql
Output:
Sql
Output:
16. IN Operator
Note: Specify multiple possible values.
Syntax: SELECT column1, column2, ... FROM table_name WHERE column IN (value1, va
lue2, ...);
Example:
Sql
SELECT first_name, last_name FROM employees WHERE department IN ('Sales', 'IT
Output:
Sql
SELECT first_name, last_name FROM employees WHERE salary BETWEEN 50000 AND 60
Output:
18. IS NULL
Note: Test for null values.
Syntax: SELECT column1, column2, ... FROM table_name WHERE column IS NULL;
Example:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Output:
24. String Functions - LOWER
Note: Convert string to lowercase.
Syntax: SELECT LOWER(column) FROM table_name;
Example:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
SELECT first_name, DAY(NOW()) AS day_of_month FROM employees;
Output:
Sql
Output:
Sql
Output:
Output:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Output:
Sql
Sql
Output:
Sql
Output:
Sql
Output:
41. Subquery
Note: Query within another query.
Syntax: SELECT column1, (SELECT column2 FROM table_name2 WHERE condition) AS ali
as FROM table_name1;
Example:
Sql
Output:
Sql
Output:
43. UNION
Note: Combine the results of two queries.
Syntax: `SELECT
Sql
Output:
44. EXCEPT
Note: Return distinct rows from the first query that are not present in the second query.
Syntax: SELECT column1, column2 FROM table_name1 EXCEPT SELECT column1, column2
FROM table_name2;
Example:
Sql
Output:
45. INTERSECT
Note: Return distinct rows that are common to both queries.
Syntax: SELECT column1, column2 FROM table_name1 INTERSECT SELECT column1, colum
n2 FROM table_name2;
Example:
Sql
Output:
Sql
Output:
Sql
Output: This example assumes the presence of another table departments with depart
ment_id and department_name columns.
Sql
Output: This example assumes the presence of another table departments with commo
n columns.
Sql
Output: This example assumes the presence of another table departments with depart
ment_id and department_name columns.
50. COALESCE
Note: Return the first non-null value.
Syntax: SELECT COALESCE(column1, column2, ...) FROM table_name;
Example:
Sql
Output: