Location via proxy:
[ UP ]
[Report a bug]
[Manage cookies]
No cookies
No scripts
No ads
No referrer
Show this form
Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
178 views
SQL For Everyone (Definitive Guide)
SQL
Uploaded by
Pradeep Kumar
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save SQL for Everyone(Definitive Guide) For Later
Download
Save
Save SQL for Everyone(Definitive Guide) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
178 views
SQL For Everyone (Definitive Guide)
SQL
Uploaded by
Pradeep Kumar
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save SQL for Everyone(Definitive Guide) For Later
Carousel Previous
Carousel Next
Save
Save SQL for Everyone(Definitive Guide) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 10
Search
Fullscreen
SQL FOR EVERYONE THE DEFINITIVE GUIDE [NOTES GALLERY (CREATE BY - ATUL KUMAR (LINKEDIN) Table of Contents . Introduction to SQL - Basic SQL Syntax - Querying Data . Filtering and Sorting Data . Joining Tables . Aggregation Functions . Subqueries and Nested Queries . Modifying Database Information . Advanced SQL Techniques 10. Optimization and Performance Tuning 1. Introduction to SQL SQL is a standard language designed for managing data in relational databases. It's commonly used to query, insert, update, and modify data. Most RDBMS (Relational Database Management System) like MySQL, SQLite, Oracle, and PostgreSQL use SQL. As a data analyst, you'll often work with large volumes of data stored in these databases. SQL becomes an essential tool to retrieve, manipulate, and analyze this data. 1.1 RDBMS and Tables In SQL, data is stored in tables, just like an Excel spreadsheet. A table is made up of rows (records) and columns (fields). Here's an example of a table, Employees:FirstName Position John Doe Analyst Jane Doe Engineer Mary Johnson Manager 2. Basic SQL Syntax Let's look at the fundamental SQL commands: SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY. 2.4 SELECT and FROM The SELECT statement is used to select data from a database, and the FROM statement specifies which table to get the data from. SELECT FirstName, LastName FROM Employees; This query retrieves all first and last names from the Employees table. If you want to select all columns, use the * symbol: SELECT * FROM Employees; 2.2 WHERE The WHERE clause is used to filter records: SELECT * FROM Employees WHERE Position = ‘Analyst’ This query retrieves all data for employees who are analysts. (NOTES GALLERY2.3 GROUP BY and HAVING GROUP BY groups rows that have the same values in specified columns into aggregated data. HAVING is used instead of WHERE with aggregated data. SELECT Position, COUNT(*) ROM Employees GROUP BY Posit HAVING COUNT(*) > 13 This query shows positions held by more than one employee. 2.4 ORDER BY ORDER BY is used to sort the data in ascending or descending order: SELECT * FROM Employees ORDER BY LastName ASC; This query sorts employees by their last name in ascending order. 3. Querying Data The SELECT statement is not just for selecting simple rows. We can use it to perform calculations, concatenations, and more. SELECT FirstName || ' ' || LastName as FullName, Position FROM Employees; This query concatenates the first and last names, separated by a space, and displays it as FullName. 4. Filtering and Sorting Data Apart from WHERE and ORDER BY, SQL offers BETWEEN, LIKE, and IN to filter data. S]NOTES GALLERY4.1 BETWEEN BETWEEN is used to filter by a range: SELECT * FROM Orders WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31'; This query selects all orders placed in the year 2023. 4.2 LIKE and ILIKE LIKE is used in a WHERE clause to search for a specified pattern in a column. The "%" sign is used to define wildcards (missing letters) both before and after the pattern. Also, note that LIKE is case sensitive. ILIKE can be used for case-insensitive search. SELECT * FROM Employees WHERE FirstName LIKE '3%"; This query selects all employees with a first name starting with ‘J’. 4.3 IN IN allows you to specify multiple values in a WHERE clause: SELECT * FROM Employees WHERE Position IN (‘Analyst', ‘Engineer’); (NOTES GALLERY This query selects all analysts and engineers. 5. Joining Tables JOIN statements are used to combine rows from two or more tables based on a related column. The different types of joins include INNER JOIN, LEFT (OUTER) JOIN, RIGHT (OUTER) JOIN, and FULL (OUTER) JOIN.Consider this additional table, Departments: DepartmentID Departmentiiame [ar | Sales [a And suppose we add a DepartmentID field to the Employees table. Here's how we can use different types of joins: 5.1 INNER JOIN SELECT Employees.LastName, Employees.FirstName, Departments .DepartmentName FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; This query retrieves the list of employees along with their respective department names. 5.2 LEFT (OUTER) JOIN SELECT Employees.LastName, Employees.FirstName, Departments. DepartmentName FROM Employees LEFT JOIN Departments ON Employees.DepartmentID = Departments .DepartmentID; This query retrieves all employees and their departments, including employees with no department (the DepartmentName for them will be NULL). 5.3 RIGHT (OUTER) JOIN SELECT Employees.LastName, Employees.FirstName, Departments.DepartmentName FROM Employees RIGHT JOIN Departments ON Employees.DepartmentID = Departments .DepartmentID; This query retrieves all departments and their employees, including departments with no employees.5.4 FULL (OUTER) JOIN SELECT Employees.LastName, Employees.FirstName, Departments .DepartmentName FROM Employees FULL JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; This query retrieves all combinations of employees and departments, including employees with no department and departments with no employees . 6. Aggregation Functions SQL provides several functions to perform calculations on data, such as COUNT(), SUM(), AVG(), MIN(), MAX(), and GROUP_CONCAT(). SELECT COUNT(*) FROM Orders, WHERE OrderDate BETWEEN '2023-@1-01' AND ‘2023-12-31'; This query returns the total number of orders placed in the year 2023. 7. Subqueries and Nested Queries A subquery is a SQL query nested inside a larger query. A subquery may oceur in: @ A SELECT clause A FROM clause A WHERE clause The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery. SELECT EmployeeID, FirstName, Position FROM Employees WHERE EmployeeID IN (SELECT EmployeeID FROM Orders WHERE OrderTotal > 1000); This query selects all employees who have made orders totaling more than 1000.8. Modifying Database Information SQL allows you to insert, update, and delete data with INSERT, UPDATE, and DELETE commands respectively. Be careful when using these commands as you can change your data permanently. 8.1 INSERT INSERT INTO Employees (EmployeeID, FirstName, LastName, Position) VALUES (4, ‘Mark’, ‘Anderson’, ‘Analyst'); This query adds a new row to the Employees table. 8.2 UPDATE UPDATE Employees SET Position = ‘Senior Analyst’ WHERE EmployeeID = 4; This query changes Mark Anderson's position to Senior Analyst. 8.3 DELETE DELETE FROM Employees WHERE EmployeeID = 4; This query deletes Mark Anderson's record from the Employees table. 9. Advanced SQL Techniques Let's delve into more complex techniques with the help of examples. 9.1 Handling NULL values NULL value in SQL means no or zero value. Here's how you can use IS NULL and IS NOT NULL: SELECT * FROM Employees WHERE DepartmentID IS NULL; This query selects all employees who don't belong to any department.SELECT * FROM Employees WHERE DepartmentID IS NOT NULL; This query selects all employees who belong to a department. 9.2 String Functions SQL offers several functions to manipulate strings. Some examples include: * CONCAT(): Concatenates two or more strings. © TRIM(): Removes leading and trailing spaces of a string. * LENGTH(): Returns the length of a string. SELECT CONCAT(FirstName, ' ', LastName) as FullName, TRIM(Position), LENGTH(FirstName) as NameLength FROM Employees; This query retrieves a full name by combining first and last names, the position after removing leading and trailing spaces, and the length of the first name. 9.3 Date and Time Functions SQL provides many functions to work with date and time. Some examples include: NOW(): Returns the current date and time. * CURDATE(): Returns the current date. CURTIME(): Returns the current time. SELECT OrderID, OrderTotal, NOW() as QueryTime FROM Orders. WHERE OrderDate = CURDATE(); This query retrieves today’s orders along with the query execution time.9.4 Case Statements Case statements help in implementing conditional logic in SQL: SELECT FirstName, Position, CASE WHEN Position = ‘Analyst’ THEN ‘Junior Level’ WHEN Position = ‘Engineer’ THEN ‘Mid Level’ ELSE ‘Senior Level’ END as JobLevel FROM Employees; This query categorizes employees into job levels based on their positions. 9.5 Window Functions Window functions perform calculations across a set of table rows that are related to the current row: SELECT FirstName, Position, Salary, RANK() OVER (PARTITION BY Position ORDER BY Salary DESC) as Rank FROM Employees; This query ranks employees within their respective positions based on their salaries. 10. Optimization and Performance Tuning Here are some examples demonstrating SQL optimization techniques: 10.1 EXPLAIN Most SQL databases support the EXPLAIN command, which shows the execution plan of an SQL statement. This can help you understand how your SQL query will be executed and where you can optimize it. EXPLAIN SELECT * FROM Employees;10.2 Avoid SELECT * Rather than using SELECT *, specify the columns you need. This reduces the amount of data that needs to be read from the disk. SELECT FirstName, LastName FROM Employees; 10.3 Use LIMIT If you only need a specific number of rows, use LIMIT to prevent reading unnecessary data. SELECT * FROM Employees ORDER BY Salary DESC LIMIT 10; This query gets the top 10 employees with the highest salaries. 10.4 Index your data Indexing your data can significantly speed up data retrieval times. Here's how you can add an index: CREATE INDEX idx_employees_position ON Employees(Position); NOTES GALLERY CREATE BY - ATUL KUMAR (LINKEDIN)
You might also like
Project 4 SQL Queries
PDF
No ratings yet
Project 4 SQL Queries
28 pages
Iti Pdfs
PDF
No ratings yet
Iti Pdfs
10 pages
SQL Vs PySpark 1678871778
PDF
No ratings yet
SQL Vs PySpark 1678871778
8 pages
Your Intermediate Guide To SQL
PDF
No ratings yet
Your Intermediate Guide To SQL
20 pages
Data Analysis With SQL: Mysql Cheat Sheet
PDF
No ratings yet
Data Analysis With SQL: Mysql Cheat Sheet
4 pages
MySQL Cheatsheet - CodeWithHarry
PDF
100% (1)
MySQL Cheatsheet - CodeWithHarry
13 pages
Jnu Dbms Lab File
PDF
No ratings yet
Jnu Dbms Lab File
55 pages
Cleaning Dirty Data With Pandas & Python - DevelopIntelligence Blog PDF
PDF
No ratings yet
Cleaning Dirty Data With Pandas & Python - DevelopIntelligence Blog PDF
8 pages
SQL & NoSQL Cheat Sheet
PDF
No ratings yet
SQL & NoSQL Cheat Sheet
52 pages
Python Question Bank Complete 100 Question
PDF
No ratings yet
Python Question Bank Complete 100 Question
23 pages
Rank, Dense Rank
PDF
100% (1)
Rank, Dense Rank
3 pages
SQL Queries and PL/SQL
PDF
No ratings yet
SQL Queries and PL/SQL
92 pages
Bayesian Machine Learning
PDF
No ratings yet
Bayesian Machine Learning
127 pages
SQL-NOSQL CHEAT Sheet
PDF
No ratings yet
SQL-NOSQL CHEAT Sheet
5 pages
Basic SQL: ITCS 201 Web Programming Part II
PDF
No ratings yet
Basic SQL: ITCS 201 Web Programming Part II
29 pages
SQL Refresher Complete Notes PDF
PDF
No ratings yet
SQL Refresher Complete Notes PDF
352 pages
SQL Exercise
PDF
No ratings yet
SQL Exercise
11 pages
Mongodb Cheat Sheet
PDF
No ratings yet
Mongodb Cheat Sheet
10 pages
Snow SQL
PDF
No ratings yet
Snow SQL
3 pages
DataStage Faq S
PDF
No ratings yet
DataStage Faq S
57 pages
100 SQL Formulas Each Student Should Know
PDF
No ratings yet
100 SQL Formulas Each Student Should Know
10 pages
Database Systems Scse
PDF
No ratings yet
Database Systems Scse
80 pages
MySQL-Full Notes
PDF
No ratings yet
MySQL-Full Notes
51 pages
Introduction To: What Is SQL?
PDF
No ratings yet
Introduction To: What Is SQL?
25 pages
International Indian School, Riyadh WORKSHEET (2020-2021) Grade - Xii - Informatics Practices - Second Term
PDF
No ratings yet
International Indian School, Riyadh WORKSHEET (2020-2021) Grade - Xii - Informatics Practices - Second Term
9 pages
Day64 - Pandas Interview Questions
PDF
No ratings yet
Day64 - Pandas Interview Questions
5 pages
Python Syllbus by Lokesh
PDF
No ratings yet
Python Syllbus by Lokesh
5 pages
Data Cleaning in SQL
PDF
No ratings yet
Data Cleaning in SQL
21 pages
SQL Information
PDF
No ratings yet
SQL Information
90 pages
Python Variables and Data Types
PDF
No ratings yet
Python Variables and Data Types
23 pages
PYTHON notes by devaraj
PDF
100% (1)
PYTHON notes by devaraj
40 pages
Mastering SQL Window Functions - 01
PDF
No ratings yet
Mastering SQL Window Functions - 01
39 pages
Big Query Optimization Document
PDF
No ratings yet
Big Query Optimization Document
10 pages
Python Pandas Cheatsheety
PDF
No ratings yet
Python Pandas Cheatsheety
7 pages
SQL Questions
PDF
100% (1)
SQL Questions
28 pages
TCS Interview Tips
PDF
No ratings yet
TCS Interview Tips
5 pages
Pyspark Basic Tasks
PDF
No ratings yet
Pyspark Basic Tasks
8 pages
Introduction To SQL
PDF
100% (1)
Introduction To SQL
67 pages
Pandas Guide
PDF
No ratings yet
Pandas Guide
64 pages
Day65 - Day70 Power BI Interview
PDF
No ratings yet
Day65 - Day70 Power BI Interview
31 pages
SQL Scenario Based Interview Questions - ThinkETL
PDF
100% (2)
SQL Scenario Based Interview Questions - ThinkETL
23 pages
SQL Syntax
PDF
No ratings yet
SQL Syntax
321 pages
Advance SQL
PDF
No ratings yet
Advance SQL
103 pages
EDA with Pandas
PDF
No ratings yet
EDA with Pandas
8 pages
SQL Questions
PDF
No ratings yet
SQL Questions
4 pages
Python Practice Exercise PDF
PDF
No ratings yet
Python Practice Exercise PDF
3 pages
Oracle PLSQL Notes
PDF
100% (4)
Oracle PLSQL Notes
59 pages
Create Int Varchar Date Varchar State Varchar: Emp - Piyush Employeeid Empname 30 Dob City 20 20
PDF
100% (1)
Create Int Varchar Date Varchar State Varchar: Emp - Piyush Employeeid Empname 30 Dob City 20 20
10 pages
Querying Microsoft SQL Server
PDF
No ratings yet
Querying Microsoft SQL Server
3 pages
SQL Interview
PDF
No ratings yet
SQL Interview
73 pages
Top 60 SQL Queries
PDF
100% (1)
Top 60 SQL Queries
34 pages
Window Function in Pyspark
PDF
100% (1)
Window Function in Pyspark
8 pages
Data Analytics Master
PDF
No ratings yet
Data Analytics Master
36 pages
Step by Step: Creating A ETL Process in MS SQL Server Integration Services (SSIS)
PDF
No ratings yet
Step by Step: Creating A ETL Process in MS SQL Server Integration Services (SSIS)
11 pages
SQL For Everyone
PDF
No ratings yet
SQL For Everyone
11 pages
SQL For Everyone
PDF
No ratings yet
SQL For Everyone
11 pages
SQL Theory With Query
PDF
No ratings yet
SQL Theory With Query
11 pages
SQL Tutorial for Beginners
PDF
No ratings yet
SQL Tutorial for Beginners
10 pages
SQL
PDF
No ratings yet
SQL
7 pages
Chapter 2 - SQL Basics and Query Optimization
PDF
No ratings yet
Chapter 2 - SQL Basics and Query Optimization
23 pages
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
Project 4 SQL Queries
PDF
Project 4 SQL Queries
Iti Pdfs
PDF
Iti Pdfs
SQL Vs PySpark 1678871778
PDF
SQL Vs PySpark 1678871778
Your Intermediate Guide To SQL
PDF
Your Intermediate Guide To SQL
Data Analysis With SQL: Mysql Cheat Sheet
PDF
Data Analysis With SQL: Mysql Cheat Sheet
MySQL Cheatsheet - CodeWithHarry
PDF
MySQL Cheatsheet - CodeWithHarry
Jnu Dbms Lab File
PDF
Jnu Dbms Lab File
Cleaning Dirty Data With Pandas & Python - DevelopIntelligence Blog PDF
PDF
Cleaning Dirty Data With Pandas & Python - DevelopIntelligence Blog PDF
SQL & NoSQL Cheat Sheet
PDF
SQL & NoSQL Cheat Sheet
Python Question Bank Complete 100 Question
PDF
Python Question Bank Complete 100 Question
Rank, Dense Rank
PDF
Rank, Dense Rank
SQL Queries and PL/SQL
PDF
SQL Queries and PL/SQL
Bayesian Machine Learning
PDF
Bayesian Machine Learning
SQL-NOSQL CHEAT Sheet
PDF
SQL-NOSQL CHEAT Sheet
Basic SQL: ITCS 201 Web Programming Part II
PDF
Basic SQL: ITCS 201 Web Programming Part II
SQL Refresher Complete Notes PDF
PDF
SQL Refresher Complete Notes PDF
SQL Exercise
PDF
SQL Exercise
Mongodb Cheat Sheet
PDF
Mongodb Cheat Sheet
Snow SQL
PDF
Snow SQL
DataStage Faq S
PDF
DataStage Faq S
100 SQL Formulas Each Student Should Know
PDF
100 SQL Formulas Each Student Should Know
Database Systems Scse
PDF
Database Systems Scse
MySQL-Full Notes
PDF
MySQL-Full Notes
Introduction To: What Is SQL?
PDF
Introduction To: What Is SQL?
International Indian School, Riyadh WORKSHEET (2020-2021) Grade - Xii - Informatics Practices - Second Term
PDF
International Indian School, Riyadh WORKSHEET (2020-2021) Grade - Xii - Informatics Practices - Second Term
Day64 - Pandas Interview Questions
PDF
Day64 - Pandas Interview Questions
Python Syllbus by Lokesh
PDF
Python Syllbus by Lokesh
Data Cleaning in SQL
PDF
Data Cleaning in SQL
SQL Information
PDF
SQL Information
Python Variables and Data Types
PDF
Python Variables and Data Types
PYTHON notes by devaraj
PDF
PYTHON notes by devaraj
Mastering SQL Window Functions - 01
PDF
Mastering SQL Window Functions - 01
Big Query Optimization Document
PDF
Big Query Optimization Document
Python Pandas Cheatsheety
PDF
Python Pandas Cheatsheety
SQL Questions
PDF
SQL Questions
TCS Interview Tips
PDF
TCS Interview Tips
Pyspark Basic Tasks
PDF
Pyspark Basic Tasks
Introduction To SQL
PDF
Introduction To SQL
Pandas Guide
PDF
Pandas Guide
Day65 - Day70 Power BI Interview
PDF
Day65 - Day70 Power BI Interview
SQL Scenario Based Interview Questions - ThinkETL
PDF
SQL Scenario Based Interview Questions - ThinkETL
SQL Syntax
PDF
SQL Syntax
Advance SQL
PDF
Advance SQL
EDA with Pandas
PDF
EDA with Pandas
SQL Questions
PDF
SQL Questions
Python Practice Exercise PDF
PDF
Python Practice Exercise PDF
Oracle PLSQL Notes
PDF
Oracle PLSQL Notes
Create Int Varchar Date Varchar State Varchar: Emp - Piyush Employeeid Empname 30 Dob City 20 20
PDF
Create Int Varchar Date Varchar State Varchar: Emp - Piyush Employeeid Empname 30 Dob City 20 20
Querying Microsoft SQL Server
PDF
Querying Microsoft SQL Server
SQL Interview
PDF
SQL Interview
Top 60 SQL Queries
PDF
Top 60 SQL Queries
Window Function in Pyspark
PDF
Window Function in Pyspark
Data Analytics Master
PDF
Data Analytics Master
Step by Step: Creating A ETL Process in MS SQL Server Integration Services (SSIS)
PDF
Step by Step: Creating A ETL Process in MS SQL Server Integration Services (SSIS)
SQL For Everyone
PDF
SQL For Everyone
SQL For Everyone
PDF
SQL For Everyone
SQL Theory With Query
PDF
SQL Theory With Query
SQL Tutorial for Beginners
PDF
SQL Tutorial for Beginners
SQL
PDF
SQL
Chapter 2 - SQL Basics and Query Optimization
PDF
Chapter 2 - SQL Basics and Query Optimization