0% found this document useful (0 votes)
5 views42 pages

SQL For Data Science

The document is a tutorial on SQL, covering key concepts such as aliases, joins, group by, having clauses, order by, and subqueries. It explains different types of joins (INNER, LEFT, RIGHT, FULL, SELF) and how to use them, alongside the importance of grouping and filtering data with aggregate functions. Additionally, it discusses the differences between subqueries and joins, and introduces the integration of SQL with Python.

Uploaded by

nj555523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views42 pages

SQL For Data Science

The document is a tutorial on SQL, covering key concepts such as aliases, joins, group by, having clauses, order by, and subqueries. It explains different types of joins (INNER, LEFT, RIGHT, FULL, SELF) and how to use them, alongside the importance of grouping and filtering data with aggregate functions. Additionally, it discusses the differences between subqueries and joins, and introduces the integration of SQL with Python.

Uploaded by

nj555523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

shivamprajapati1734@gmail.

com
7OPHDBMN8E

SQL Tutorial

Sampriti Chatterjee (Great Learning)


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Alias in SQL

Alias concept is used in SQL to give temporary name to the table or the column of the

table

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Joins In SQL
Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
What is join in SQL?

A JOIN concept which is basically a clause used to combine rows from different tables

based on a condition

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Types of join in SQL

INNER JOIN

01
Types of join in SQL LEFT JOIN
02
[email protected]
7OPHDBMN8E

SELF JOIN 05 03 RIGHT JOIN


04
FULL JOIN

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Types of join in SQL?

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
INNER JOIN

The INNER JOIN helps to select the matching records from the both tables

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for Inner join In SQL


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
LEFT JOIN

The LEFT JOIN helps to fetch all records from the left table and the matching records

from the right table

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for LEFT join In SQL


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
RIGHT JOIN

The RIGHT JOIN helps to fetch all records from the right table and the matched records

from the left table

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for RIGHT join In SQL


Proprietary
This file content. © Great
isProprietary
meant for Learning. Alluse
personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only. prohibited
content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Sharing or publishing the contents in part or full is liable for legal action.
FULL JOIN

The FULL OUTER JOIN helps to fetch all the records from both the table whether there is

a match or not

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for FULL join In SQL


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
SELF JOIN

A self helps to join with itself

[email protected]
7OPHDBMN8E

Table join with itself

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for SELF join In SQL


Proprietary
This file content. © Great
isProprietary
meant for Learning. Alluse
personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only. prohibited
content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Group by and Having concept in SQL


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Group By

Group by statement helps to groups rows which has similar properties

Most of the time


[email protected]
7OPHDBMN8E group by function is used
with aggregate function like: count, MAX,
MIN etc.

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for Group by statement


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Having Clause

Having clause has an important role in SQL when we use any aggregate function we can

not use where clause and instead of that we use having clause

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for Having Clause


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Order By

Order by command is used to sort the data in an ascending order or in a descending

order

[email protected]
 By default order
7OPHDBMN8E by sorts the data into ascending

order

 And to sort the data into descending order need to

use desc keyword

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for Order by statement


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Subquery in SQL
Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
How to handle two or
more queries at a
time?

Do I need to write
two separate query?
[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
What is subquery?

Subquery is a SQL command which is nothing but select query that contain another

query inside

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
How many parts are there in a
subquery?

Subquery

[email protected]
7OPHDBMN8E

Inner Query Outer Query

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
How subquery executes?

First inner query get executed

Get the result from inner query

[email protected]
7OPHDBMN8E

Output of inner query added to the outer query

Outer query will get executed to get the final

result

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Types of subquery

 Nested subquery: Nested subquery first executes the

inner SELECT query and then with the returning values executes

the outer query


[email protected]
7OPHDBMN8E

 Correlated subquery: A correlated subquery reads every row

in a table and comparing values in each row against related data

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

Demo for Subqueries


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Subquery Vs join

Topic Subquery Join

A Subquery or Inner query or Nested


A join is nothing but a query which
Description query is also a query within SQL query
combines records from different tables
and comes after the WHERE clause

Subqueries break the complex query


Fetching data using join concept is
into isolated parts in order to make a
Handling complex
[email protected] queries faster
7OPHDBMN8E complex query into a series of logical
steps.
Easy to understand and code Joins are not easy to read like
Code maintenance
maintenance is also at ease. subqueries

Subqueries help or allow to use the As we have different types of joins so it


Result results of another query in the outer is difficult to understand which one will
query. give you the proper result

Subquery is easier to work with but


Optimizer Join is the good optimizer by server
optimization is not good
Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

SQL with Python


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Introduction to Python

Python is a popular high level, object oriented and interpreted language

[email protected]
7OPHDBMN8E
High level Interpreted

Object oriented
Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
History of Python

But, why the Important Facts


language called as • Python is invented by Guido van Rossum
Python?
in 1989
• Rossum used to love watching comedy
movies from late seventies

[email protected] • He needed a short, unique, and slightly


7OPHDBMN8E
mysterious name for his language
• In that time he was watching Monty
Inventor of Python Python’s Flying Circus and from that
series he decided to keep his language
name python.
• This how Python invented

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Why should you learn Python?

Python is simple and beginner


3 friendly language

1 Web development using


Python
[email protected]
7OPHDBMN8E

5 Graphical user interface


4

2 Length of the program is


Mathematical computation
short
can be done easily
Proprietary
This file content. © Great
isProprietary
meant for Learning. Alluse
personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only. prohibited
content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution
Sharing or publishing the contents in part or full is liable for legal action.
Why Python is so popular?

1 Largest community for Learners and Collaborators 2 Open source

Easy to learn and usable flexibility


[email protected] Huge numbers of Python libraries and
3
7OPHDBMN8E 4
Frame work

5 Supports Big Data, Machine Learning 6


Supports Automation
and Cloud computing

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Installing Python

This is the site to install Python -> https://www.python.org/downloads/

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Popular IDE for Python: Pycharm

Site to install Python ->


https://www.jetbrains.com/pycharm/download/#section=mac

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
isProprietary
meant for Learning. Alluse
personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only. prohibited
content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution
Sharing or publishing the contents in part or full is liable for legal action.
Popular IDE for Python: Anaconda

Anaconda installation site->


https://www.anaconda.com/products/individual

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Popular IDE for Python: Google colab

Google collaboratory link->


https://colab.research.google.com/notebooks/intro.ipynb

[email protected]
7OPHDBMN8E

Proprietary
This file content. © Great
isProprietary
meant for Learning. Alluse
personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only. prohibited
content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E

SQL with Python


Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
7OPHDBMN8E
Thank You

Proprietary
This file content. © Great
is meant Learning. Alluse
for personal Rights
byReserved. Unauthorized use or distribution prohibited.
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.

You might also like