0% found this document useful (0 votes)
9 views

DATA ANALYSIS - SQL

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DATA ANALYSIS - SQL

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

DATA ANALYSIS

Module 2: Structured Query Language (SQL)

OCTOBER 7, 2024
FLOH TECHNOLOGIES
Jesus Pavilion Plaza, Plot 113, Cele B/s, Ijede Road, Ikorodu, Lagos.
TABLE OF CONTENT

Introduction to SQL..............................................................................................................................................................5
What is SQL?..................................................................................................................................................................... 5
Importance of SQL.............................................................................................................................................................5
SQL Standards...................................................................................................................................................................5
Setting Up Your Environment...............................................................................................................................................5
Installing a Database Management System (DBMS)..........................................................................................................5
Choosing a SQL Client........................................................................................................................................................6
Connecting to a Database.................................................................................................................................................6
Basic SQL Concepts...............................................................................................................................................................6
Databases and Tables........................................................................................................................................................6
Rows and Columns............................................................................................................................................................6
Data Types.........................................................................................................................................................................6
CRUD Operations.................................................................................................................................................................. 6
Creating a Database..........................................................................................................................................................6
Creating a Table................................................................................................................................................................6
Reading Data.....................................................................................................................................................................7
Updating Data...................................................................................................................................................................7
Deleting Data..................................................................................................................................................................... 7
Filtering and Sorting Data.....................................................................................................................................................7
WHERE Clause...................................................................................................................................................................7
ORDER BY Clause...............................................................................................................................................................7
LIMIT Clause......................................................................................................................................................................7
Joins...................................................................................................................................................................................... 7
INNER JOIN........................................................................................................................................................................ 7
LEFT JOIN........................................................................................................................................................................... 8
RIGHT JOIN........................................................................................................................................................................8
FULL OUTER JOIN..............................................................................................................................................................8
Aggregate Functions.............................................................................................................................................................8
COUNT, SUM, AVG, MIN, MAX..........................................................................................................................................8
GROUP BY Clause..............................................................................................................................................................9
HAVING Clause.................................................................................................................................................................. 9
Subqueries............................................................................................................................................................................ 9
Introduction to Subqueries...............................................................................................................................................9
Using Subqueries...............................................................................................................................................................9
Indexes.................................................................................................................................................................................. 9
What are Indexes?............................................................................................................................................................9
Creating and Using Indexes...............................................................................................................................................9
Benefits and Trade-offs.....................................................................................................................................................9
Views.................................................................................................................................................................................. 10
What are Views?.............................................................................................................................................................10
Creating and Managing Views.........................................................................................................................................10
Advantages of Using Views.............................................................................................................................................10
Stored Procedures and Functions......................................................................................................................................10
What are Stored Procedures?.........................................................................................................................................10
Creating and Calling Stored Procedures..........................................................................................................................10
User-Defined Functions...................................................................................................................................................10
Transactions........................................................................................................................................................................ 10
Understanding Transactions............................................................................................................................................10
COMMIT and ROLLBACK..................................................................................................................................................10
ACID Properties...............................................................................................................................................................11
Best Practices......................................................................................................................................................................11
Writing Readable SQL Code.............................................................................................................................................11
Performance Optimization..............................................................................................................................................11
Security Considerations...................................................................................................................................................11
Introduction to SQL
What is SQL?
SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational
databases. It allows users to create, read, update, and delete data.

Importance of SQL
SQL is crucial for data analysis, application development, and data management. It is widely used across various
industries and is a fundamental skill for data professionals.

SQL Standards
SQL has several standards (SQL-89, SQL-92, SQL-99, SQL-2003, SQL-2008, SQL-2011), with most databases supporting a
subset of these features.

Setting Up Your Environment


Installing a Database Management System (DBMS)
Popular DBMS options include:

i. MySQL
ii. PostgreSQL
iii. Microsoft SQL Server
iv. SQLite
v. Oracle Database

Choosing a SQL Client


i. Choose a SQL client based on your DBMS. Popular options include:
ii. MySQL Workbench (MySQL)
iii. pgAdmin (PostgreSQL)
iv. SQL Server Management Studio (SQL Server)
v. DBeaver (Multiple databases)

Connecting to a Database
Follow the specific instructions for your DBMS and SQL client to establish a connection to your database.

Basic SQL Concepts


Databases and Tables
i. Database: A structured collection of data.
ii. Table: A collection of related data entries consisting of rows and columns.

Rows and Columns


i. Row: A single record in a table.
ii. Column: A set of data values of a particular type.

Data Types
Common data types include:

i. Integer
ii. Float
iii. String (VARCHAR, CHAR)
iv. Date and Time (DATE, TIME, TIMESTAMP)

CRUD Operations
Creating a Database
```sql

CREATE DATABASE database_name;

```

Creating a Table
```sql

CREATE TABLE table_name (

column1 data_type,

column2 data_type,

...

);

```

Reading Data
```sql

SELECT column1, column2 FROM table_name;

```

Updating Data
```sql

UPDATE table_name

SET column1 = value1

WHERE condition;

```

Deleting Data
```sql

DELETE FROM table_name WHERE condition;

```
Filtering and Sorting Data
WHERE Clause
```sql

SELECT * FROM table_name WHERE condition;

```

ORDER BY Clause
```sql

SELECT * FROM table_name ORDER BY column1 [ASC|DESC];

```

LIMIT Clause
```sql

SELECT * FROM table_name LIMIT number;

```

Joins
INNER JOIN
```sql

SELECT columns

FROM table1

INNER JOIN table2 ON table1.column_name = table2.column_name;

```

LEFT JOIN
```sql

SELECT columns

FROM table1

LEFT JOIN table2 ON table1.column_name = table2.column_name;

```

RIGHT JOIN
```sql

SELECT columns

FROM table1

RIGHT JOIN table2 ON table1.column_name = table2.column_name;

```
FULL OUTER JOIN
```sql

SELECT columns

FROM table1

FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;

```

Aggregate Functions
COUNT, SUM, AVG, MIN, MAX
```sql

SELECT COUNT(column_name) FROM table_name;

SELECT SUM(column_name) FROM table_name;

SELECT AVG(column_name) FROM table_name;

SELECT MIN(column_name) FROM table_name;

SELECT MAX(column_name) FROM table_name;

```

GROUP BY Clause
```sql

SELECT column_name, COUNT(*)

FROM table_name

GROUP BY column_name;

```

HAVING Clause
```sql

SELECT column_name, COUNT(*)

FROM table_name

GROUP BY column_name

HAVING COUNT(*) > value;

```

Subqueries
Introduction to Subqueries
A subquery is a query nested inside another query.
Using Subqueries
```sql

SELECT column_name

FROM table_name

WHERE column_name IN (SELECT column_name FROM another_table);

```

Indexes
What are Indexes?
Indexes are used to speed up the retrieval of rows by creating a data structure that allows for faster lookups.

Creating and Using Indexes


```sql

CREATE INDEX index_name ON table_name (column_name);

```

Benefits and Trade-offs


Indexes improve read performance but can slow down write operations.

Views
What are Views?
A view is a virtual table based on the result of a SELECT query.

Creating and Managing Views


```sql

CREATE VIEW view_name AS

SELECT column1, column2 FROM table_name WHERE condition;

```

Advantages of Using Views


i. Simplify complex queries
ii. Enhance security by restricting access to specific data

Stored Procedures and Functions


What are Stored Procedures?
Stored procedures are precompiled SQL statements stored in the database.

Creating and Calling Stored Procedures


```sql

CREATE PROCEDURE procedure_name AS


BEGIN

-- SQL statements

END;

```

User-Defined Functions
Functions can return a single value or a table.

Transactions
Understanding Transactions
A transaction is a sequence of operations performed as a single logical unit of work.

COMMIT and ROLLBACK


```sql

BEGIN TRANSACTION;

-- SQL statements

COMMIT; -- Save changes

ROLLBACK; -- Undo changes

```

ACID Properties
i. Atomicity: All or nothing.
ii. Consistency: Valid state before and after.
iii. Isolation: Transactions don't interfere with each other.
iv. Durability: Changes persist after a commit.

Best Practices
Writing Readable SQL Code
i. Use proper indentation and comments.
ii. Use meaningful table and column names.

Performance Optimization
i. Use indexes appropriately.
ii. Avoid SELECT *; specify needed columns.

Security Considerations
i. Use parameterized queries to prevent SQL injection.
ii. Limit user permissions.

You might also like