0% found this document useful (0 votes)
2 views11 pages

SQL Complete Notes

These notes provide a comprehensive overview of SQL, covering its use in managing relational databases, including commands for data manipulation, querying, and performance optimization. Key topics include SELECT statements, filtering data, aggregate functions, joins, subqueries, and indexing. The document also emphasizes SQL's importance for Data Analysts in tasks such as data extraction, cleaning, and aggregation.

Uploaded by

suralemanoj
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)
2 views11 pages

SQL Complete Notes

These notes provide a comprehensive overview of SQL, covering its use in managing relational databases, including commands for data manipulation, querying, and performance optimization. Key topics include SELECT statements, filtering data, aggregate functions, joins, subqueries, and indexing. The document also emphasizes SQL's importance for Data Analysts in tasks such as data extraction, cleaning, and aggregation.

Uploaded by

suralemanoj
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

SQL – Complete Notes with Examples

These notes provide a deep understanding of Structured Query Language (SQL) used for managing and
analyzing relational databases. The notes cover database concepts, queries, joins, aggregations, subqueries,
indexing, and real-world examples useful for Data Analyst interviews.
1. Introduction to SQL

SQL (Structured Query Language) is used to manage and manipulate relational databases.

Databases store structured data in tables consisting of rows and columns.

Example Table: Employees

Emp_ID | Name | Department | Salary 101 | John | Sales | 50000 102 | Sarah | HR | 60000

SQL allows users to: • Retrieve data • Insert data • Update records • Delete records
2. Types of SQL Commands

SQL commands are categorized into several groups.

DDL (Data Definition Language) Used to define database structure.

Examples: CREATE ALTER DROP TRUNCATE

DML (Data Manipulation Language) Used to manipulate data.

Examples: INSERT UPDATE DELETE

DQL (Data Query Language) Used to retrieve data.

Example: SELECT

DCL (Data Control Language) GRANT REVOKE


3. SELECT Statement

SELECT is the most commonly used SQL command.

Basic Syntax:

SELECT column_name FROM table_name;

Example:

SELECT Name, Salary FROM Employees;

This query retrieves the Name and Salary columns from the Employees table.
4. Filtering Data using WHERE

The WHERE clause filters records based on conditions.

Example:

SELECT * FROM Employees WHERE Salary > 50000;

Operators: = Equal > Greater than < Less than >= Greater than equal to <= Less than equal to <> Not equal

Example:

SELECT * FROM Employees WHERE Department = 'Sales';


5. Aggregate Functions

Aggregate functions perform calculations on multiple rows.

Common Functions:

COUNT() – Counts rows SUM() – Adds values AVG() – Calculates average MAX() – Highest value MIN() –
Lowest value

Example:

SELECT SUM(Salary) FROM Employees;

This calculates the total salary of all employees.


6. GROUP BY and HAVING

GROUP BY groups rows with similar values.

Example:

SELECT Department, SUM(Salary) FROM Employees GROUP BY Department;

Output:

Department | Total Salary Sales | 150000 HR | 120000

HAVING filters aggregated results.

Example:

SELECT Department, SUM(Salary) FROM Employees GROUP BY Department HAVING SUM(Salary) > 100000;
7. Joins in SQL

Joins combine data from multiple tables.

Types of Joins:

INNER JOIN Returns matching records from both tables.

LEFT JOIN Returns all rows from left table and matching rows from right table.

RIGHT JOIN Returns all rows from right table.

Example:

SELECT [Link], [Link] FROM Customers INNER JOIN Orders ON [Link] =


[Link];
8. Subqueries

A subquery is a query inside another query.

Example:

SELECT Name FROM Employees WHERE Salary > ( SELECT AVG(Salary) FROM Employees );

This query retrieves employees earning more than the average salary.
9. Indexes and Performance

Indexes improve database query performance.

An index works like an index in a book, allowing the database to find data quickly.

Example:

CREATE INDEX idx_salary ON Employees(Salary);

Benefits: • Faster query execution • Efficient searching


10. SQL for Data Analysts

SQL is one of the most important tools for Data Analysts.

Typical tasks include:

• Extracting data from databases • Cleaning and filtering data • Joining datasets • Aggregating business metrics

Example Business Query:

SELECT Region, SUM(Sales) FROM Sales_Data GROUP BY Region ORDER BY SUM(Sales) DESC;

This query identifies the regions with the highest sales.

You might also like