data analyst SQL
data analyst SQL
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
1. SELECT Statement:
- Used to fetch data from a table.
- Syntax: SELECT column1, column2 FROM table_name;
Joins in SQL are used to combine data from multiple tables based on a related column.
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;
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);
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)
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. 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).
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
1. Indexing:
- Speeds up searches by storing data pointers.
- Syntax: CREATE INDEX index_name ON table_name(column_name);
3. Partitioning:
- Splits large tables into smaller, manageable chunks.
- Types: RANGE, LIST, HASH, COMPOSITE.
SQL Notes - Day 12: Stored Procedures & Triggers
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
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
1. Working on a Dataset:
- Importing and exploring data.
INSERT INTO employees (id, name, salary) VALUES (1, 'Alice', 50000), (2, 'Bob', 60000);
## 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;