0% found this document useful (0 votes)
3 views16 pages

SQL Queries

The document provides a comprehensive guide on SQL SELECT queries for an employees table, detailing various commands such as basic SELECT, SELECT ALL, WHERE clause, JOINs, and aggregation functions. Each command is accompanied by notes, syntax, examples, and expected outputs. It covers a wide range of SQL functionalities, including filtering, sorting, grouping, and string and date manipulation.

Uploaded by

harshithajanga12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

SQL Queries

The document provides a comprehensive guide on SQL SELECT queries for an employees table, detailing various commands such as basic SELECT, SELECT ALL, WHERE clause, JOINs, and aggregation functions. Each command is accompanied by notes, syntax, examples, and expected outputs. It covers a wide range of SQL functionalities, including filtering, sorting, grouping, and string and date manipulation.

Uploaded by

harshithajanga12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

employees :

employee_id first_name last_name department salary

1 John Doe Sales 50000

2 Jane Smith Marketing 60000

3 Emily Johnson Sales 55000

4 Michael Brown IT 75000

5 Sarah Davis Marketing 62000

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

SELECT first_name, last_name FROM employees;

Output:

2. SELECT ALL
Note: Retrieve all columns.
Syntax: SELECT * FROM table_name;
Example:

Sql

SELECT * FROM employees;

Output:

3. SELECT DISTINCT
Note: Retrieve unique values.
Syntax: SELECT DISTINCT column1 FROM table_name;
Example:

Sql

SELECT DISTINCT department FROM employees;

Output:

4. WHERE Clause
Note: Filter records.
Syntax: SELECT column1, column2, ... FROM table_name WHERE condition;
Example:

Sql

SELECT first_name, last_name FROM employees WHERE department = 'Sales';

Output:

5. ORDER BY Clause
Note: Sort results.
Syntax: SELECT column1, column2, ... FROM table_name ORDER BY column1 ASC|DESC;
Example:

Sql

SELECT first_name, last_name FROM employees ORDER BY last_name ASC;

Output:

6. LIMIT Clause
Note: Limit the number of records.
Syntax: SELECT column1, column2, ... FROM table_name LIMIT number;
Example:

Sql

SELECT first_name, last_name FROM employees LIMIT 3;

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

SELECT employees.first_name, departments.department_name


FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id

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

SELECT department, COUNT(*) FROM employees GROUP BY department;

Output:

9. HAVING Clause
Note: Filter groups.
Syntax: SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING conditi
on;
Example:

Sql

SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*

Output:

10. Aggregation Functions


Note: Perform calculations on data.
Syntax: SELECT AGG_FUNC(column) FROM table_name;
Example:

Sql

SELECT AVG(salary) FROM employees;

Output:

11. SUM Function

Note: Calculate the sum of a column.


Syntax: SELECT SUM(column) FROM table_name;
Example:

Sql

SELECT SUM(salary) FROM employees;

Output:

12. MIN Function


Note: Find the minimum value.
Syntax: SELECT MIN(column) FROM table_name;
Example:

Sql

SELECT MIN(salary) FROM employees;

Output:
13. MAX Function
Note: Find the maximum value.
Syntax: SELECT MAX(column) FROM table_name;
Example:

Sql

SELECT MAX(salary) FROM employees;

Output:

14. COUNT Function


Note: Count the number of rows.
Syntax: SELECT COUNT(column) FROM table_name;
Example:

Sql

SELECT COUNT(*) FROM employees;

Output:

15. LIKE Operator


Note: Search for a pattern.
Syntax: SELECT column1, column2, ... FROM table_name WHERE column LIKE pattern;
Example:

Sql

SELECT first_name, last_name FROM employees WHERE last_name LIKE 'S%';

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:

17. BETWEEN Operator


Note: Select values within a range.
Syntax: SELECT column1, column2, ... FROM table_name WHERE column BETWEEN value1
AND value2;
Example:

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

SELECT first_name, last_name FROM employees WHERE department IS NULL;

Output:

19. IS NOT NULL


Note: Test for non-null values.
Syntax: SELECT column1, column2, ... FROM table_name WHERE column IS NOT NULL;
Example:

Sql

SELECT first_name, last_name FROM employees WHERE department IS NOT NULL;

Output:

20. String Functions - CONCAT


Note: Concatenate two or more strings.
Syntax: SELECT CONCAT(string1, string2, ...) FROM table_name;
Example:

Sql

SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;

Output:

21. String Functions - LENGTH


Note: Get the length of a string.
Syntax: SELECT LENGTH(column) FROM table_name;
Example:

Sql

SELECT first_name, LENGTH(first_name) AS name_length FROM employees;

Output:

22. String Functions - SUBSTRING


Note: Extract a substring.
Syntax: SELECT SUBSTRING(column, start, length) FROM table_name;
Example:

Sql

SELECT first_name, SUBSTRING(first_name, 1, 2) AS short_name FROM employees;

Output:

23. String Functions - UPPER


Note: Convert string to uppercase.
Syntax: SELECT UPPER(column) FROM table_name;
Example:

Sql

SELECT first_name, UPPER(first_name) AS upper_name FROM employees;

Output:
24. String Functions - LOWER
Note: Convert string to lowercase.
Syntax: SELECT LOWER(column) FROM table_name;
Example:

Sql

SELECT first_name, LOWER(first_name) AS lower_name FROM employees;

Output:

25. Date Functions - CURRENT_DATE


Note: Get the current date.
Syntax: SELECT CURRENT_DATE;
Example:

Sql

SELECT CURRENT_DATE AS today;

Output:

26. Date Functions - DATE_FORMAT


Note: Format date values.
Syntax: SELECT DATE_FORMAT(column, format) FROM table_name;
Example:

Sql

SELECT first_name, DATE_FORMAT(NOW(), '%W, %d %M %Y') AS current_date FROM em

Output:

27. Date Functions - DAY


Note: Extract the day of the month.
Syntax: SELECT DAY(column) FROM table_name;
Example:

Sql
SELECT first_name, DAY(NOW()) AS day_of_month FROM employees;

Output:

28. Date Functions - MONTH


Note: Extract the month.
Syntax: SELECT MONTH(column) FROM table_name;
Example:

Sql

SELECT first_name, MONTH(NOW()) AS month_of_year FROM employees;

Output:

29. Date Functions - YEAR

Certainly! Let's continue from where we left off:


29. Date Functions - YEAR
Note: Extract the year.
Syntax: SELECT YEAR(column) FROM table_name;
Example:

Sql

SELECT first_name, YEAR(NOW()) AS current_year FROM employees;

Output:

30. Date Functions - HOUR


Note: Extract the hour.
Syntax: SELECT HOUR(column) FROM table_name;
Example:
Sql

SELECT first_name, HOUR(NOW()) AS current_hour FROM employees;

Output:

31. Date Functions - MINUTE


Note: Extract the minute.
Syntax: SELECT MINUTE(column) FROM table_name;
Example:

Sql

SELECT first_name, MINUTE(NOW()) AS current_minute FROM employees;

Output:

32. Date Functions - SECOND


Note: Extract the second.
Syntax: SELECT SECOND(column) FROM table_name;
Example:

Sql

SELECT first_name, SECOND(NOW()) AS current_second FROM employees;

Output:

33. Mathematical Functions - ABS


Note: Return the absolute value.
Syntax: SELECT ABS(column) FROM table_name;
Example:

Sql

SELECT ABS(-salary) AS absolute_salary FROM employees;

Output:

34. Mathematical Functions - ROUND


Note: Round to a specified number of decimal places.
Syntax: SELECT ROUND(column, decimals) FROM table_name;
Example:

Sql

SELECT first_name, ROUND(salary, -3) AS rounded_salary FROM employees;

Output:

35. Mathematical Functions - FLOOR


Note: Round down to the nearest integer.
Syntax: SELECT FLOOR(column) FROM table_name;
Example:

Sql

SELECT first_name, FLOOR(salary / 1000) AS salary_in_thousands FROM employees

Output:

36. Mathematical Functions - CEIL


Note: Round up to the nearest integer.
Syntax: SELECT CEIL(column) FROM table_name;
Example:

Sql

SELECT first_name, CEIL(salary / 1000) AS salary_in_thousands FROM employees;

Output:

37. Mathematical Functions - POWER


Note: Raise a number to the power of another number.
Syntax: SELECT POWER(column, exponent) FROM table_name;
Example:

Sql

SELECT first_name, POWER(salary, 2) AS salary_squared FROM employees;


Output:

38. Mathematical Functions - SQRT


Note: Return the square root.
Syntax: SELECT SQRT(column) FROM table_name;
Example:

Sql

SELECT first_name, SQRT(salary) AS salary_sqrt FROM employees;

Output:

39. Mathematical Functions - LOG


Note: Return the natural logarithm.
Syntax: SELECT LOG(column) FROM table_name;
Example:

Sql

SELECT first_name, LOG(salary) AS salary_log FROM employees;

Output:

40. Case Statement


Note: Perform conditional logic.
Syntax: SELECT CASE WHEN condition THEN result ELSE result END FROM table_name;
Example:

Sql

SELECT first_name, salary,


CASE
WHEN salary < 55000 THEN 'Low'
WHEN salary >= 55000 AND salary < 70000 THEN 'Medium'
ELSE 'High'
END AS salary_range
FROM employees;

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

SELECT first_name, salary,


(SELECT AVG(salary) FROM employees) AS avg_salary
FROM employees;

Output:

42. Correlated Subquery


Note: Subquery that references columns from the outer query.
Syntax: SELECT column1, column2 FROM table_name1 WHERE column3 = (SELECT column4
FROM table_name2 WHERE condition);
Example:

Sql

SELECT first_name, salary


FROM employees e1
WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e1.department = e2

Output:

43. UNION
Note: Combine the results of two queries.
Syntax: `SELECT

Certainly! Here are commands 43 to 50:


43. UNION
Note: Combine the results of two queries.
Syntax: SELECT column1, column2 FROM table_name1 UNION SELECT column1, column2 F
ROM table_name2;
Example:

Sql

SELECT first_name, last_name FROM employees WHERE department = 'Sales'


UNION
SELECT first_name, last_name FROM employees WHERE department = 'IT';

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

SELECT first_name, last_name FROM employees WHERE department = 'Sales'


EXCEPT
SELECT first_name, last_name FROM employees WHERE last_name = 'Doe';

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

SELECT first_name, last_name FROM employees WHERE department = 'Sales'


INTERSECT
SELECT first_name, last_name FROM employees WHERE last_name = 'Johnson';

Output:

46. Self JOIN


Note: Join a table to itself.
Syntax: SELECT a.column1, b.column2 FROM table_name a, table_name b WHERE a.comm
on_column = b.common_column;
Example:

Sql

SELECT a.first_name AS Employee1, b.first_name AS Employee2


FROM employees a, employees b
WHERE a.department = b.department AND a.employee_id != b.employee_id;

Output:

47. FULL OUTER JOIN


Note: Return all rows when there is a match in one of the tables.
Syntax: SELECT column1, column2 FROM table1 FULL OUTER JOIN table2 ON table1.com
mon_column = table2.common_column;
Example:

Sql

SELECT a.first_name, b.department_name


FROM employees a
FULL OUTER JOIN departments b ON a.department_id = b.department_id;

Output: This example assumes the presence of another table departments with depart
ment_id and department_name columns.

48. NATURAL JOIN


Note: Join tables based on all columns with the same name and datatypes.
Syntax: SELECT columns FROM table1 NATURAL JOIN table2;
Example:

Sql

SELECT * FROM employees NATURAL JOIN departments;

Output: This example assumes the presence of another table departments with commo
n columns.

49. CROSS JOIN


Note: Return the Cartesian product of two tables.
Syntax: SELECT columns FROM table1 CROSS JOIN table2;
Example:

Sql

SELECT a.first_name, b.department_name


FROM employees a
CROSS JOIN departments b;

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

SELECT first_name, COALESCE(NULL, salary, 0) AS effective_salary FROM employe

Output:

You might also like