├── README.md ├── SQL.pdf ├── SQLQuery1.sql ├── answer.docx ├── answer.pdf ├── questions & topic.txt └── sql learn.pdf /README.md: -------------------------------------------------------------------------------- 1 | # SQL Practice Tutorial 2 | 3 | Welcome to the **SQL Practice Tutorial** repository! This tutorial is designed to help you master SQL through practical exercises and examples. 4 | 5 | ## Author 6 | 7 | **MD Fahim Shahoriar Titu** 8 | 9 | ## Table of Contents 10 | 11 | 1. [Introduction](#introduction) 12 | 2. [Getting Started](#getting-started) 13 | 3. [Tutorial Overview](#tutorial-overview) 14 | 4. [What You Will Achieve](#what-you-will-achieve) 15 | 5. [Contributing](#contributing) 16 | 6. [License](#license) 17 | 18 | ## Introduction 19 | 20 | Welcome to the SQL Practice Tutorial! This repository contains a comprehensive guide to SQL, covering everything from basic data retrieval to advanced data manipulation and performance optimization. 21 | 22 | ## Getting Started 23 | 24 | To get started with this tutorial, clone the repository to your local machine: 25 | 26 | ```bash 27 | git clone https://github.com/your-username/sql-practice-tutorial.git 28 | cd sql-practice-tutorial 29 | ``` 30 | 31 | ## Tutorial Overview 32 | 33 | This tutorial is divided into several sections, each focusing on different aspects of SQL: 34 | 35 | 1. **Table Creation and Data Insertion**: Learn how to create tables and insert data. 36 | 2. **Basic Data Retrieval**: Understand how to retrieve data using SELECT statements. 37 | 3. **Filtering Data**: Use WHERE clauses to filter data. 38 | 4. **Sorting Data**: Order your query results. 39 | 5. **Aggregating Data**: Perform aggregate functions like COUNT, AVG, MIN, and MAX. 40 | 6. **Grouping Data**: Group data and perform operations on groups. 41 | 7. **Joining Tables**: Combine data from multiple tables using JOINs. 42 | 8. **Subqueries**: Use subqueries to perform complex queries. 43 | 9. **Advanced Queries**: Implement CASE statements, COALESCE, and UNION. 44 | 10. **Updating Data**: Learn how to update existing data. 45 | 11. **Deleting Data**: Remove data from your tables. 46 | 12. **Merging Data**: Use MERGE for upsert operations. 47 | 13. **Advanced Data Retrieval**: Use CTEs, recursive CTEs, and PIVOT. 48 | 14. **Advanced Data Manipulation**: Implement triggers, stored procedures, and transactions. 49 | 15. **Performance Optimization**: Optimize your queries with indexes and EXPLAIN. 50 | 16. **Security and Permissions**: Manage user permissions. 51 | 17. **Data Export and Import**: Export and import data using CSV files. 52 | 53 | ## What You Will Achieve 54 | 55 | By completing this tutorial, you will: 56 | 57 | - **Master SQL Basics**: Understand the fundamental concepts of SQL. 58 | - **Perform Complex Queries**: Execute advanced queries and data manipulations. 59 | - **Optimize Performance**: Learn techniques to optimize query performance. 60 | - **Ensure Data Integrity**: Use transactions and constraints to maintain data integrity. 61 | - **Manage Database Security**: Handle user permissions and secure your database. 62 | 63 | ## Contributing 64 | 65 | Contributions are welcome! Please read the [contributing guidelines](CONTRIBUTING.md) before submitting a pull request. 66 | 67 | ## License 68 | 69 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 70 | -------------------------------------------------------------------------------- /SQL.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MdFahimShahoriar/SQL-Practice/cb3d2767b666aa46f52a6d031730a134e5662d9d/SQL.pdf -------------------------------------------------------------------------------- /SQLQuery1.sql: -------------------------------------------------------------------------------- 1 | create database book; 2 | use book; 3 | -- Create the Authors table first 4 | CREATE TABLE Authors ( 5 | AuthorID INT PRIMARY KEY, 6 | Name VARCHAR(255) NOT NULL, 7 | BirthYear INT 8 | ); 9 | 10 | -- Create the Books table after Authors table 11 | CREATE TABLE Books ( 12 | BookID INT PRIMARY KEY, 13 | Title VARCHAR(255) NOT NULL, 14 | AuthorID INT, 15 | Genre VARCHAR(100), 16 | PublishedYear INT, 17 | FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID) 18 | ); 19 | 20 | -- Create the Borrowers table 21 | CREATE TABLE Borrowers ( 22 | BorrowerID INT PRIMARY KEY, 23 | Name VARCHAR(255) NOT NULL, 24 | Address VARCHAR(255), 25 | PhoneNumber VARCHAR(20) 26 | ); 27 | -- Insert sample data into Authors table 28 | INSERT INTO Authors (AuthorID, Name, BirthYear) VALUES 29 | (1, 'J.K. Rowling', 1965), 30 | (2, 'George Orwell', 1903), 31 | (3, 'J.R.R. Tolkien', 1892), 32 | (4, 'Agatha Christie', 1890), 33 | (5, 'Stephen King', 1947), 34 | (6, 'Mark Twain', 1835), 35 | (7, 'Jane Austen', 1775), 36 | (8, 'Charles Dickens', 1812), 37 | (9, 'Ernest Hemingway', 1899), 38 | (10, 'F. Scott Fitzgerald', 1896); 39 | 40 | -- Insert sample data into Books table 41 | INSERT INTO Books (BookID, Title, AuthorID, Genre, PublishedYear) VALUES 42 | (1, 'Harry Potter and the Philosopher''s Stone', 1, 'Fantasy', 1997), 43 | (2, '1984', 2, 'Dystopian', 1949), 44 | (3, 'The Hobbit', 3, 'Fantasy', 1937), 45 | (4, 'Murder on the Orient Express', 4, 'Mystery', 1934), 46 | (5, 'The Shining', 5, 'Horror', 1977), 47 | (6, 'Adventures of Huckleberry Finn', 6, 'Adventure', 1884), 48 | (7, 'Pride and Prejudice', 7, 'Romance', 1813), 49 | (8, 'A Tale of Two Cities', 8, 'Historical', 1859), 50 | (9, 'The Old Man and the Sea', 9, 'Literary Fiction', 1952), 51 | (10, 'The Great Gatsby', 10, 'Tragedy', 1925); 52 | 53 | -- Insert sample data into Borrowers table 54 | INSERT INTO Borrowers (BorrowerID, Name, Address, PhoneNumber) VALUES 55 | (1, 'Alice Johnson', '123 Maple St', '555-1234'), 56 | (2, 'Bob Smith', '456 Oak St', '555-5678'), 57 | (3, 'Charlie Brown', '789 Pine St', '555-8765'), 58 | (4, 'Daisy Miller', '101 Birch St', '555-4321'), 59 | (5, 'Eve Davis', '202 Cedar St', '555-6789'), 60 | (6, 'Frank Wilson', '303 Elm St', '555-9876'), 61 | (7, 'Grace Lee', '404 Spruce St', '555-3456'), 62 | (8, 'Hank Green', '505 Willow St', '555-6543'), 63 | (9, 'Ivy White', '606 Fir St', '555-7890'), 64 | (10, 'Jack Black', '707 Ash St', '555-0987'); 65 | --Select all data from each table 66 | SELECT * FROM Books; 67 | SELECT * FROM Authors; 68 | SELECT * FROM Borrowers; 69 | --Show all columns and their details for each table 70 | SELECT column_name, data_type, is_nullable, column_default 71 | FROM information_schema.columns 72 | WHERE table_name = 'Books'; 73 | -- 74 | SELECT column_name, data_type, is_nullable, column_default 75 | FROM information_schema.columns 76 | WHERE table_name = 'Authors'; 77 | -- 78 | SELECT column_name, data_type, is_nullable, column_default 79 | FROM information_schema.columns 80 | WHERE table_name = 'Borrowers'; 81 | 82 | --Basic Data Retrieval 83 | --Select all columns from a table: 84 | SELECT * FROM Books; 85 | --Select specific columns from a table 86 | SELECT Title, Genre FROM Books; 87 | --Select distinct values: 88 | SELECT DISTINCT Genre FROM Books; 89 | --Filtering Data 90 | --Filter rows using the WHERE clause 91 | SELECT * FROM Books WHERE Genre = 'Fantasy'; 92 | --Filter rows using multiple conditions: 93 | SELECT * FROM Books WHERE Genre = 'Fantasy' AND PublishedYear < 2000; 94 | --Filter rows using the IN operator: 95 | SELECT * FROM Books WHERE Genre IN ('Fantasy', 'Mystery'); 96 | --Sorting Data 97 | --Order results by a column: 98 | SELECT * FROM Books ORDER BY PublishedYear; 99 | --Order results by multiple columns: 100 | SELECT * FROM Books ORDER BY Genre, PublishedYear DESC; 101 | SELECT * FROM Books ORDER BY Genre, PublishedYear ASC; 102 | --Aggregating Data 103 | --Count the number of rows: 104 | SELECT COUNT(*) FROM Books; 105 | --Calculate the average value: 106 | SELECT AVG(PublishedYear) AS AverageYear FROM Books; 107 | --Find the minimum and maximum values: 108 | SELECT MIN(PublishedYear) AS OldestBook, MAX(PublishedYear) AS NewestBook FROM Books; 109 | --Grouping Data 110 | --Group by a column and count: 111 | SELECT Genre, COUNT(*) AS NumberOfBooks FROM Books GROUP BY Genre; 112 | --Group by multiple columns: 113 | SELECT Genre, PublishedYear, COUNT(*) AS NumberOfBooks FROM Books GROUP BY Genre, PublishedYear; 114 | --Joining Tables 115 | --Inner join between Books and Authors: 116 | SELECT Books.Title, Authors.Name 117 | FROM Books 118 | INNER JOIN Authors ON Books.AuthorID = Authors.AuthorID; 119 | --Left join between Books and Authors: 120 | SELECT Books.Title, Authors.Name 121 | FROM Books 122 | LEFT JOIN Authors ON Books.AuthorID = Authors.AuthorID; 123 | --Subqueries 124 | --Subquery in the WHERE clause: 125 | SELECT * FROM Books 126 | WHERE AuthorID IN (SELECT AuthorID FROM Authors WHERE BirthYear < 1900); 127 | --Subquery in the SELECT clause 128 | SELECT Title, (SELECT Name FROM Authors WHERE Authors.AuthorID = Books.AuthorID) AS AuthorName 129 | FROM Books; 130 | --Advanced Queries 131 | --Using CASE statements: 132 | SELECT Title, 133 | CASE 134 | WHEN PublishedYear < 2000 THEN 'Old' 135 | ELSE 'New' 136 | END AS BookAge 137 | FROM Books; 138 | --Using COALESCE to handle NULL values: 139 | SELECT Title, COALESCE(Genre, 'Unknown') AS Genre 140 | FROM Books; 141 | --Using UNION to combine results from multiple queries: 142 | SELECT Title FROM Books WHERE Genre = 'Fantasy' 143 | UNION 144 | SELECT Title FROM Books WHERE PublishedYear > 2000; 145 | --Updating Data 146 | --Update a single column: 147 | UPDATE Books 148 | SET Genre = 'Classic' 149 | WHERE PublishedYear < 1950; 150 | --Update multiple columns: 151 | UPDATE Authors 152 | SET Name = 'Samuel Clemens', BirthYear = 1835 153 | WHERE AuthorID = 6; 154 | --Deleting Data 155 | --Delete specific rows: 156 | DELETE FROM Borrowers 157 | WHERE BorrowerID = 10; 158 | --Delete all rows from a table: 159 | --DELETE FROM Books;-- 160 | --Merging Data 161 | --Merge (upsert) data 162 | MERGE INTO Books AS target 163 | USING (SELECT 1 AS BookID, 'New Book' AS Title, 1 AS AuthorID, 'Fiction' AS Genre, 2024 AS PublishedYear) AS source 164 | ON (target.BookID = source.BookID) 165 | WHEN MATCHED THEN 166 | UPDATE SET Title = source.Title, Genre = source.Genre, PublishedYear = source.PublishedYear 167 | WHEN NOT MATCHED THEN 168 | INSERT (BookID, Title, AuthorID, Genre, PublishedYear) 169 | VALUES (source.BookID, source.Title, source.AuthorID, source.Genre, source.PublishedYear); 170 | --Advanced Data Retrieval 171 | --Using LIKE for pattern matching: 172 | SELECT * FROM Books 173 | WHERE Title LIKE '%Harry Potter%'; 174 | --Using BETWEEN for range filtering 175 | SELECT * FROM Books 176 | WHERE PublishedYear BETWEEN 1900 AND 2010; 177 | --Using EXISTS to check for the existence of rows: 178 | SELECT * FROM Authors 179 | WHERE EXISTS (SELECT 1 FROM Books WHERE Books.AuthorID = Authors.AuthorID AND Genre = 'Fantasy'); 180 | --Window Functions 181 | --Using ROW_NUMBER for ranking: 182 | SELECT Title, PublishedYear, 183 | ROW_NUMBER() OVER (ORDER BY PublishedYear DESC) AS RowNum 184 | FROM Books; 185 | --Using SUM with PARTITION BY 186 | SELECT AuthorID, SUM(PublishedYear) OVER (PARTITION BY AuthorID) AS TotalYears 187 | FROM Books; 188 | --Advanced Data Retrieval 189 | --Using CTE (Common Table Expressions): 190 | WITH BookCounts AS ( 191 | SELECT AuthorID, COUNT(*) AS NumberOfBooks 192 | FROM Books 193 | GROUP BY AuthorID 194 | ) 195 | SELECT Authors.Name, BookCounts.NumberOfBooks 196 | FROM Authors 197 | JOIN BookCounts ON Authors.AuthorID = BookCounts.AuthorID; 198 | --Using Recursive CTE: 199 | WITH Numbers AS ( 200 | SELECT 1 AS Number 201 | UNION ALL 202 | SELECT Number + 1 203 | FROM Numbers 204 | WHERE Number < 10 205 | ) 206 | SELECT Number FROM Numbers 207 | OPTION (MAXRECURSION 10); 208 | --Using PIVOT to transform data: 209 | SELECT * 210 | FROM ( 211 | SELECT Genre, PublishedYear 212 | FROM Books 213 | ) AS SourceTable 214 | PIVOT ( 215 | COUNT(PublishedYear) 216 | FOR PublishedYear IN ([1997], [1949], [1937], [1934], [1977], [1884], [1813], [1859], [1952], [1925]) 217 | ) AS PivotTable; 218 | --Advanced Data Manipulation 219 | --Using TRIGGERS to automate actions: 220 | CREATE TRIGGER trgAfterInsert 221 | ON Borrowers 222 | AFTER INSERT 223 | AS 224 | BEGIN 225 | PRINT 'A new borrower has been added.' 226 | END; 227 | --Using STORED PROCEDURES for reusable code: 228 | CREATE PROCEDURE GetBooksByAuthor 229 | @AuthorID INT 230 | AS 231 | BEGIN 232 | SELECT * FROM Books WHERE AuthorID = @AuthorID; 233 | END; 234 | --Using TRANSACTIONS to ensure data integrity 235 | BEGIN TRANSACTION; 236 | UPDATE Books SET Genre = 'Classic' WHERE PublishedYear < 1950; 237 | DELETE FROM Borrowers WHERE BorrowerID = 10; 238 | COMMIT TRANSACTION; 239 | --Performance Optimization 240 | --Creating INDEXES to speed up queries 241 | CREATE INDEX idx_books_genre ON Books (Genre); 242 | --Using EXPLAIN to analyze query performance: 243 | SET SHOWPLAN_ALL ON; 244 | GO 245 | 246 | SELECT * FROM Books WHERE Genre = 'Fantasy'; 247 | GO 248 | 249 | SET SHOWPLAN_ALL OFF; 250 | GO 251 | -- 252 | SET SHOWPLAN_XML ON; 253 | GO 254 | 255 | SELECT * FROM Books WHERE Genre = 'Fantasy'; 256 | GO 257 | 258 | SET SHOWPLAN_XML OFF; 259 | GO 260 | 261 | --Security and Permissions 262 | --Granting and revoking permissions: 263 | --GRANT SELECT, INSERT ON Books TO [titu];-- 264 | --REVOKE INSERT ON Books FROM [titu];-- 265 | --Data Export and Import 266 | --Exporting data to a CSV file 267 | BCP "SELECT * FROM Books" queryout "C:\Books.csv" -c -t, -T -S [SQLQuery1]; 268 | --Importing data from a CSV file: 269 | BULK INSERT Books 270 | FROM 'C:\Books.csv' 271 | WITH ( 272 | FIELDTERMINATOR = ',', 273 | ROWTERMINATOR = '\n' 274 | ); 275 | 276 | 277 | -------------------------------------------------------------------------------- /answer.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MdFahimShahoriar/SQL-Practice/cb3d2767b666aa46f52a6d031730a134e5662d9d/answer.docx -------------------------------------------------------------------------------- /answer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MdFahimShahoriar/SQL-Practice/cb3d2767b666aa46f52a6d031730a134e5662d9d/answer.pdf -------------------------------------------------------------------------------- /questions & topic.txt: -------------------------------------------------------------------------------- 1 | 1. create table 2 | 2. insert sample data 3 | 3. --Select all data from each table 4 | 4. --Show all columns and their details for each table 5 | 5. Basic Data Retrieval 6 | --Select all columns from a table: 7 | --Select specific columns from a table 8 | --Select distinct values: 9 | 6. --Filtering Data 10 | --Filter rows using the WHERE clause 11 | --Filter rows using multiple conditions: 12 | --Filter rows using the IN operator: 13 | 7. --Sorting Data 14 | --Order results by a column: 15 | --Order results by multiple columns: 16 | 8. --Aggregating Data 17 | --Count the number of rows: 18 | --Calculate the average value: 19 | --Find the minimum and maximum values: 20 | 9. --Grouping Data 21 | --Group by a column and count: 22 | --Group by multiple columns: 23 | 10. --Joining Tables 24 | --Inner join between Books and Authors: 25 | --Left join between Books and Authors: 26 | 11. --Subqueries 27 | --Subquery in the WHERE clause: 28 | --Subquery in the SELECT clause 29 | 12. --Advanced Queries 30 | --Using CASE statements: 31 | --Using COALESCE to handle NULL values: 32 | --Using UNION to combine results from multiple queries: 33 | 13. --Updating Data 34 | --Update a single column: 35 | --Update multiple columns: 36 | 14. --Deleting Data 37 | --Delete specific rows: 38 | --Delete all rows from a table: 39 | 15. --Merging Data 40 | --Merge (upsert) data 41 | 16. --Advanced Data Retrieval 42 | --Using LIKE for pattern matching: 43 | --Using BETWEEN for range filtering 44 | --Using EXISTS to check for the existence of rows: 45 | 17. --Window Functions 46 | --Using ROW_NUMBER for ranking: 47 | --Using SUM with PARTITION BY 48 | 18. --Advanced Data Retrieval 49 | --Using CTE (Common Table Expressions): 50 | --Using Recursive CTE: 51 | --Using PIVOT to transform data: 52 | 19.--Advanced Data Manipulation 53 | --Using TRIGGERS to automate actions: 54 | --Using STORED PROCEDURES for reusable code: 55 | --Using TRANSACTIONS to ensure data integrity 56 | 20. --Performance Optimization 57 | --Creating INDEXES to speed up queries 58 | --Using EXPLAIN to analyze query performance: 59 | 21. --Security and Permissions 60 | --Granting and revoking permissions: 61 | 22. --Data Export and Import 62 | --Exporting data to a CSV file 63 | --Importing data from a CSV file: 64 | -------------------------------------------------------------------------------- /sql learn.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MdFahimShahoriar/SQL-Practice/cb3d2767b666aa46f52a6d031730a134e5662d9d/sql learn.pdf --------------------------------------------------------------------------------