0% found this document useful (0 votes)
4 views8 pages

DQL Guide Obsidian

The document provides an overview of SQL statements and clauses for data retrieval and manipulation, including SELECT, WHERE, ORDER BY, LIMIT, and JOINs. It covers syntax, examples, and common use cases for filtering, sorting, aggregating, and combining data from multiple tables. Additionally, it explains the use of aliases, UNION, pattern matching with LIKE, and logical operators.

Uploaded by

BIRUK GEBRE
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)
4 views8 pages

DQL Guide Obsidian

The document provides an overview of SQL statements and clauses for data retrieval and manipulation, including SELECT, WHERE, ORDER BY, LIMIT, and JOINs. It covers syntax, examples, and common use cases for filtering, sorting, aggregating, and combining data from multiple tables. Additionally, it explains the use of aliases, UNION, pattern matching with LIKE, and logical operators.

Uploaded by

BIRUK GEBRE
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/ 8

1.

SELECT Statement

Fetch data from a table.

Syntax:

SELECT column1, column2, ...


FROM table_name;

Examples:

Fetch all columns:

SELECT * FROM employees;

Fetch specific columns:

SELECT first_name, last_name FROM employees;

2. WHERE Clause

Filter records based on conditions.

Syntax:

SELECT column1, column2


FROM table_name
WHERE condition;

Examples:

Filter by value:
SELECT * FROM employees WHERE department = 'IT';

Filter with comparison:

SELECT * FROM employees WHERE salary > 50000;

Combine conditions:

SELECT * FROM employees WHERE salary > 50000 AND department = 'IT';

3. ORDER BY Clause

Sort results in ascending ( ASC ) or descending ( DESC ) order.

Syntax:

SELECT column1, column2


FROM table_name
ORDER BY column1 ASC|DESC;

Examples:

Sort by salary:

SELECT * FROM employees ORDER BY salary DESC;

Sort by department and name:

SELECT * FROM employees ORDER BY department ASC, first_name ASC;

4. LIMIT Clause

Restrict the number of rows returned.


Syntax:

SELECT column1, column2


FROM table_name
LIMIT number_of_rows OFFSET start_row;

Examples:

Get top 5 employees:

SELECT * FROM employees LIMIT 5;

Skip first 5 rows and get the next 5:

SELECT * FROM employees LIMIT 5 OFFSET 5;

5. Aggregation Functions

Summarize data.

Common Functions:

COUNT() : Count rows.


SUM() : Total sum.
AVG() : Calculate average.
MAX() : Maximum value.
MIN() : Minimum value.

Examples:

Count employees:

SELECT COUNT(*) FROM employees;

Total salary:
SELECT SUM(salary) FROM employees;

Average salary:

SELECT AVG(salary) FROM employees;

6. GROUP BY Clause

Group rows based on a column and apply aggregate functions.

Syntax:

SELECT column1, aggregate_function(column2)


FROM table_name
GROUP BY column1;

Examples:

Group by department and count employees:

SELECT department, COUNT(*)


FROM employees
GROUP BY department;

Group by department and calculate total salary:

SELECT department, SUM(salary)


FROM employees
GROUP BY department;

7. HAVING Clause

Filter aggregated data (after GROUP BY ).

Examples:
Departments with more than 5 employees:

SELECT department, COUNT(*)


FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

8. JOINs

Combine rows from multiple tables.

INNER JOIN:

Fetch rows with matching values in both tables.

SELECT e.name, d.department_name


FROM employees e
INNER JOIN departments d ON e.department_id = d.id;

LEFT JOIN:

Fetch all rows from the left table and matching rows from the right.

SELECT e.name, d.department_name


FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;

RIGHT JOIN:

Fetch all rows from the right table and matching rows from the left.

SELECT e.name, d.department_name


FROM employees e
RIGHT JOIN departments d ON e.department_id = d.id;
9. Subqueries

Query inside another query.

Examples:

Employees with salary above the average:

SELECT name, salary


FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

10. Aliases

Rename columns or tables for better readability.

Examples:

Rename a column:

SELECT name AS employee_name, salary AS employee_salary


FROM employees;

Rename a table:

SELECT e.name, d.name AS department_name


FROM employees e
INNER JOIN departments d ON e.department_id = d.id;

11. UNION

Combine results of two queries.

Syntax:
SELECT column1 FROM table1
UNION
SELECT column1 FROM table2;

Examples:

Combine employees from two tables

SELECT name FROM employees_2023


UNION
SELECT name FROM employees_2024;

12. Pattern Matching with LIKE

Search using patterns.

Examples:

Names starting with "A":

SELECT * FROM employees WHERE name LIKE 'A%';


```

- Names ending with "son":

```sql
SELECT * FROM employees WHERE name LIKE '%son';

13. Logical Operators

AND: Combine conditions.


OR: Either condition can be true.
NOT: Negate a condition.

Examples:

Salary greater than 50000 and in IT department:


SELECT * FROM employees WHERE salary > 50000 AND department = 'IT';

Not in HR department:

SELECT * FROM employees WHERE NOT department = 'HR';

You might also like