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

SQL Course Complete

The document outlines a comprehensive SQL course structured from beginner to advanced levels. It covers fundamental concepts such as basic SQL commands, data manipulation, aggregation functions, and joins, progressing to advanced topics like subqueries, views, stored procedures, and performance optimization. Each section includes practical examples and SQL statements to facilitate learning and application.

Uploaded by

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

SQL Course Complete

The document outlines a comprehensive SQL course structured from beginner to advanced levels. It covers fundamental concepts such as basic SQL commands, data manipulation, aggregation functions, and joins, progressing to advanced topics like subqueries, views, stored procedures, and performance optimization. Each section includes practical examples and SQL statements to facilitate learning and application.

Uploaded by

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

SQL Course: Beginner to Advanced

Beginner Level

1. Introduction to SQL
SQL (Structured Query Language) is used for managing and manipulating relational
databases. This section introduces SQL and the tools like MySQL, PostgreSQL, SQL Server,
and SQLite.

2. Basic SQL Commands


Learn to create databases, tables, and perform basic operations.

Create a Database and Table:

CREATE DATABASE BakeryDB;


USE BakeryDB;

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Price DECIMAL(10, 2),
Stock INT
);

Insert Data:

INSERT INTO Products (ProductID, ProductName, Price, Stock)


VALUES (1, 'Sourdough Bread', 4.99, 50),
(2, 'Bagel', 1.99, 100);

Retrieve Data:

SELECT * FROM Products;

3. Basic Filtering and Sorting


Learn how to filter and sort data using WHERE and ORDER BY clauses.

WHERE Clause:

SELECT ProductName, Price


FROM Products
WHERE Price > 2.00;

ORDER BY Clause:

SELECT * FROM Products


ORDER BY Price DESC;

4. Updating and Deleting Data


Modify or delete records using UPDATE and DELETE statements.

UPDATE Statement:

UPDATE Products
SET Stock = Stock - 1
WHERE ProductName = 'Sourdough Bread';

DELETE Statement:

DELETE FROM Products


WHERE ProductID = 2;

Intermediate Level

5. Advanced Filtering
Learn to filter data using advanced techniques like IN, BETWEEN, and LIKE.

Using IN and BETWEEN:

SELECT * FROM Products


WHERE Price BETWEEN 1.00 AND 5.00;

LIKE and Wildcards:

SELECT * FROM Products


WHERE ProductName LIKE 'S%';
6. Aggregation Functions
Perform calculations on data using COUNT, AVG, SUM, and GROUP BY.

COUNT, AVG, SUM:

SELECT COUNT(*) AS TotalProducts, AVG(Price) AS AvgPrice


FROM Products;

GROUP BY:

SELECT Stock, SUM(Price) AS TotalValue


FROM Products
GROUP BY Stock;

7. Joining Tables
Combine data from multiple tables using JOIN operations.

Inner Join:

SELECT o.OrderID, p.ProductName, o.Quantity


FROM Orders o
INNER JOIN Products p ON o.ProductID = p.ProductID;

Left Join:

SELECT p.ProductName, o.Quantity


FROM Products p
LEFT JOIN Orders o ON p.ProductID = o.ProductID;

Advanced Level

8. Subqueries and Nested Queries


Use subqueries to perform advanced data retrieval.

Simple Subquery:

SELECT ProductName
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);

9. Views and Indexing


Optimize data retrieval using views and indexes.

Create a View:

CREATE VIEW ExpensiveProducts AS


SELECT ProductName, Price
FROM Products
WHERE Price > 3.00;

Using an Index:

CREATE INDEX idx_price ON Products(Price);

10. Stored Procedures and Triggers


Automate tasks using stored procedures and triggers.

Stored Procedure:

DELIMITER $$

CREATE PROCEDURE UpdateStock(IN productID INT, IN quantity INT)


BEGIN
UPDATE Products
SET Stock = Stock - quantity
WHERE ProductID = productID;
END $$

DELIMITER ;

Trigger Example:

CREATE TRIGGER AfterOrderInsert


AFTER INSERT ON Orders
FOR EACH ROW
BEGIN
UPDATE Products
SET Stock = Stock - NEW.Quantity
WHERE ProductID = NEW.ProductID;
END;

11. Transactions
Ensure data consistency using transactions.

Transaction Example:

START TRANSACTION;

UPDATE Products
SET Stock = Stock - 10
WHERE ProductID = 1;

COMMIT;

12. Performance Optimization


Learn techniques to optimize SQL queries and database performance.

Example Query Plan Analysis:

EXPLAIN SELECT * FROM Products WHERE Price > 10;

You might also like