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

data analyst SQL

The document provides a comprehensive 14-day study plan for learning SQL, covering topics from basic SQL queries to advanced concepts like performance optimization and stored procedures. Each day focuses on specific SQL functionalities, including data manipulation, aggregation, joins, and data analysis techniques. Code examples are included to illustrate practical applications of the concepts discussed.

Uploaded by

mathew creation
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)
15 views16 pages

data analyst SQL

The document provides a comprehensive 14-day study plan for learning SQL, covering topics from basic SQL queries to advanced concepts like performance optimization and stored procedures. Each day focuses on specific SQL functionalities, including data manipulation, aggregation, joins, and data analysis techniques. Code examples are included to illustrate practical applications of the concepts discussed.

Uploaded by

mathew creation
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

SQL Notes - Day 1: Introduction to SQL & Databases

What is SQL?
SQL (Structured Query Language) is a programming language used to manage and manipulate
relational databases.
It allows users to create, read, update, and delete (CRUD) data from databases efficiently.

Types of Databases
1. Relational Databases (RDBMS): Store data in tables with rows and columns. Examples: MySQL,
PostgreSQL, SQL Server.
2. NoSQL Databases: Designed for unstructured or semi-structured data. Examples: MongoDB,
Cassandra.

SQL vs NoSQL
- SQL databases use structured schemas and tables, ensuring data integrity.
- NoSQL databases are schema-less, providing flexibility for large-scale applications.

Installing MySQL/PostgreSQL
To install MySQL:
1. Download from the official MySQL website.
2. Follow installation steps and set up the root password.

To install PostgreSQL:
1. Download from the official PostgreSQL website.
2. Configure user settings and install pgAdmin.

Connecting to a Database
To connect to MySQL:
- Use MySQL Workbench or the command line:
`mysql -u root -p`

To connect to PostgreSQL:
- Use pgAdmin or the command line:
`psql -U postgres`
SQL Notes - Day 2: Basic SQL Queries

SQL queries allow us to retrieve and manipulate data stored in a database.

1. SELECT Statement:
- Used to fetch data from a table.
- Syntax: SELECT column1, column2 FROM table_name;

2. Filtering Data (WHERE, BETWEEN, IN, LIKE):


- WHERE: Filters records based on a condition.
- BETWEEN: Selects values within a range.
- IN: Matches values from a list.
- LIKE: Searches for a specified pattern.

3. Sorting Data (ORDER BY):


- Sorts query results in ascending or descending order.
- Syntax: SELECT * FROM table_name ORDER BY column_name ASC|DESC;
SQL Notes - Day 3: Aggregations & Grouping

Aggregation functions allow us to perform calculations on a set of values.

1. COUNT: Counts the number of rows.


Syntax: SELECT COUNT(*) FROM table_name;

2. SUM: Adds up numeric column values.


Syntax: SELECT SUM(column_name) FROM table_name;

3. AVG: Calculates the average of a numeric column.


Syntax: SELECT AVG(column_name) FROM table_name;

4. MIN & MAX: Find the smallest and largest values.


Syntax: SELECT MIN(column_name), MAX(column_name) FROM table_name;

5. GROUP BY & HAVING:


- GROUP BY groups rows with the same values.
- HAVING filters grouped records.
Syntax: SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name
HAVING COUNT(*) > 1;
SQL Notes - Day 4: Joins & Relationships

Joins in SQL are used to combine data from multiple tables based on a related column.

1. INNER JOIN: Returns matching records from both tables.


Syntax: SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;

2. LEFT JOIN: Returns all records from the left table and matching records from the right table.
Syntax: SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;

3. RIGHT JOIN: Returns all records from the right table and matching records from the left table.
Syntax: SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;

4. FULL OUTER JOIN: Returns all records when there is a match in either table.
Syntax: SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id;

5. CROSS JOIN: Returns the Cartesian product of both tables.

6. SELF JOIN: A table joins itself.


SQL Notes - Day 5: Subqueries & Common Table Expressions (CTEs)

Subqueries and CTEs are used to structure complex queries efficiently.

1. Subqueries:
- A query inside another query.
- Can be used in SELECT, FROM, and WHERE clauses.
- Syntax: SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM
employees);

2. EXISTS & NOT EXISTS:


- EXISTS: Returns true if a subquery returns any rows.
- NOT EXISTS: Returns true if a subquery returns no rows.

3. Common Table Expressions (CTEs):


- Temporary result sets that can be referenced in a SELECT statement.
- Syntax:
WITH cte_name AS (SELECT column1 FROM table_name)
SELECT * FROM cte_name;
SQL Notes - Day 6: Modifying Data (DML - Data Manipulation Language)

DML commands are used to modify database records.

1. INSERT: Adds new records.


Syntax: INSERT INTO table_name (column1, column2) VALUES (value1, value2);

2. UPDATE: Modifies existing records.


Syntax: UPDATE table_name SET column1 = value1 WHERE condition;

3. DELETE: Removes records.


Syntax: DELETE FROM table_name WHERE condition;

4. Transactions:
- Used to execute multiple queries as a single unit.
- Syntax:
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
SQL Notes - Day 7: Creating and Managing Database Structures (DDL)

DDL commands are used to define and manage database structures.

1. CREATE: Creates databases, tables, or views.


Syntax: CREATE TABLE table_name (column1 DATATYPE, column2 DATATYPE);

2. ALTER: Modifies an existing table structure.


Syntax: ALTER TABLE table_name ADD column_name DATATYPE;

3. DROP: Deletes databases or tables.


Syntax: DROP TABLE table_name;

4. Constraints:
- NOT NULL: Ensures a column cannot have NULL values.
- UNIQUE: Ensures all values in a column are unique.
- PRIMARY KEY: Uniquely identifies a record in a table.
- FOREIGN KEY: Establishes a relationship between two tables.
SQL Notes - Day 8: Window Functions

Window functions perform calculations across a specific range of rows related to the current row.

1. ROW_NUMBER: Assigns a unique row number.


Syntax: SELECT column1, ROW_NUMBER() OVER (PARTITION BY column2 ORDER BY
column3) AS row_num FROM table_name;

2. RANK: Assigns a ranking with gaps for duplicate values.


Syntax: SELECT column1, RANK() OVER (ORDER BY column2 DESC) FROM table_name;

3. DENSE_RANK: Assigns a ranking without gaps for duplicates.


Syntax: SELECT column1, DENSE_RANK() OVER (ORDER BY column2 DESC) FROM
table_name;

4. LEAD & LAG:


- LEAD: Fetches next row's value.
- LAG: Fetches previous row's value.
Syntax: SELECT column1, column2, LEAD(column2) OVER (ORDER BY column3) FROM
table_name;
SQL Notes - Day 9: Advanced Aggregations & Pivoting Data

Advanced aggregation techniques help analyze data more efficiently.

1. CASE Statements:
- Used for conditional logic in SQL queries.
Syntax: SELECT column1, CASE WHEN column2 > 100 THEN 'High' ELSE 'Low' END FROM
table_name;

2. Pivoting Data:
- Used to transform row-based data into column format.
- Implemented using CASE or PIVOT (SQL Server).

3. GROUPING SETS, ROLLUP, CUBE:


- GROUPING SETS: Allows multiple GROUP BY clauses in a single query.
- ROLLUP: Aggregates data hierarchically.
- CUBE: Performs all possible aggregations.
SQL Notes - Day 10: Working with Dates & Strings

Date and string functions help manipulate and format data.

1. DATE & TIME Functions:


- CURDATE(): Returns current date.
- DATEADD(): Adds days, months, years to a date.
- DATEDIFF(): Finds difference between two dates.

2. STRING Functions:
- TRIM(): Removes spaces from a string.
- SUBSTRING(): Extracts a portion of a string.
- CONCAT(): Combines multiple strings.
- REPLACE(): Replaces a part of a string.
SQL Notes - Day 11: Performance Optimization

SQL performance tuning improves query execution speed.

1. Indexing:
- Speeds up searches by storing data pointers.
- Syntax: CREATE INDEX index_name ON table_name(column_name);

2. Query Optimization & EXPLAIN:


- EXPLAIN helps analyze query execution plans.
- Syntax: EXPLAIN SELECT * FROM table_name;

3. Partitioning:
- Splits large tables into smaller, manageable chunks.
- Types: RANGE, LIST, HASH, COMPOSITE.
SQL Notes - Day 12: Stored Procedures & Triggers

Stored procedures and triggers help automate SQL processes.

1. Stored Procedures:
- Predefined SQL code that can be executed multiple times.
- Syntax:
CREATE PROCEDURE procedure_name AS BEGIN SELECT * FROM table_name; END;

2. Using Parameters:
- Parameters allow dynamic values.
- Syntax: CREATE PROCEDURE procedure_name (@param INT) AS SELECT * FROM
table_name WHERE id = @param;

3. Triggers:
- Executes automatically before or after INSERT, UPDATE, DELETE operations.
- Syntax:
CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW BEGIN
... END;
SQL Notes - Day 13: SQL for Data Analysis & Reporting

SQL is widely used for data analysis and reporting.

1. Writing Complex Queries for Analytics:


- Combining multiple joins, CTEs, and window functions.

2. Using CTEs & Window Functions for Reports:


- Enhancing readability and reusability of queries.

3. Creating Views:
- Virtual tables that store query results for future use.
- Syntax: CREATE VIEW view_name AS SELECT column1, column2 FROM table_name;
SQL Notes - Day 14: Final Project - Analyzing Real-World Data

Applying SQL knowledge on a real-world dataset.

1. Working on a Dataset:
- Importing and exploring data.

2. Writing Queries for Insights:


- Filtering, aggregations, and joins for meaningful analysis.

3. Optimizing and Presenting Results:


- Using indexes and views for better query performance.
SQL Notes - Day SQL_Codes: SQL Code Examples - 14-Day Study Plan

# SQL Code Examples - 14-Day Study Plan

## Day 1 - Introduction to SQL & Databases


CREATE DATABASE my_database;
USE my_database;

CREATE TABLE employees (


id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10,2)
);

INSERT INTO employees (id, name, salary) VALUES (1, 'Alice', 50000), (2, 'Bob', 60000);

SELECT * FROM employees;

## Day 2 - Retrieving Data with SELECT


SELECT name, salary FROM employees;
SELECT DISTINCT salary FROM employees;
SELECT * FROM employees WHERE salary > 55000;
SELECT * FROM employees ORDER BY salary DESC;

## Day 3 - Filtering & Sorting Data


SELECT * FROM employees WHERE name LIKE 'A%';
SELECT * FROM employees WHERE salary BETWEEN 50000 AND 70000;

## Day 4 - Aggregation & Grouping


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

## Day 5 - Joins
SELECT e.name, d.department_name FROM employees e
JOIN departments d ON e.department_id = d.id;
## Day 6 - Advanced Joins & Set Operations
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments
WHERE location = 'NYC');
SELECT name FROM employees UNION SELECT name FROM contractors;

## Day 7 - Subqueries & Common Table Expressions (CTEs)


WITH HighSalary AS (SELECT name, salary FROM employees WHERE salary > 60000)
SELECT * FROM HighSalary;

## Day 8 - Window Functions


SELECT name, salary, ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary
DESC) FROM employees;

## Day 9 - Advanced Aggregations & Pivoting


SELECT department, SUM(salary) FROM employees GROUP BY department WITH ROLLUP;

## Day 10 - Working with Dates & Strings


SELECT CURDATE(), DATE_ADD(CURDATE(), INTERVAL 7 DAY);
SELECT UPPER(name), TRIM(name) FROM employees;

## Day 11 - Performance Optimization


CREATE INDEX idx_salary ON employees(salary);
EXPLAIN SELECT * FROM employees WHERE salary > 50000;

## Day 12 - Stored Procedures & Triggers


CREATE PROCEDURE GetHighSalaryEmployees() AS BEGIN SELECT * FROM employees
WHERE salary > 60000; END;

## Day 13 - SQL for Data Analysis & Reporting


CREATE VIEW HighEarners AS SELECT name, salary FROM employees WHERE salary > 60000;

## Day 14 - Final Project (Real-World Analysis)


SELECT department, AVG(salary) FROM employees GROUP BY department ORDER BY
AVG(salary) DESC;

You might also like