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

SQL For Data Science

The document is a tutorial on SQL, covering key concepts such as aliases, joins (INNER, LEFT, RIGHT, FULL, and SELF JOIN), group by and having clauses, order by statements, and subqueries. It explains how these SQL features work, their purposes, and includes examples for better understanding. Additionally, it briefly introduces Python as a programming language and its history.

Uploaded by

Wesley John
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)
31 views42 pages

SQL For Data Science

The document is a tutorial on SQL, covering key concepts such as aliases, joins (INNER, LEFT, RIGHT, FULL, and SELF JOIN), group by and having clauses, order by statements, and subqueries. It explains how these SQL features work, their purposes, and includes examples for better understanding. Additionally, it briefly introduces Python as a programming language and its history.

Uploaded by

Wesley John
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

weslian1223@gmail.

com
KIXVJDEPYZ

SQL Tutorial

Sampriti Chatterjee (Great Learning)


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Joins In SQL
Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

SELF JOIN 05 03 RIGHT JOIN


04
FULL JOIN

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
Types of join in SQL?

[email protected]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for Inner join In SQL


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for LEFT join In SQL


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for RIGHT join In SQL


Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
[email protected] prohibited.
Proprietary content. ©Great Learning. All Reserved. Unauthorized use or distribution prohibited
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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for FULL join In SQL


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Table join with itself

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for SELF join In SQL


Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
[email protected] prohibited.
Proprietary content. ©Great Learning. All Reserved. Unauthorized use or distribution prohibited
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]
KIXVJDEPYZ

Group by and Having concept in SQL


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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
[email protected]
KIXVJDEPYZ time group by function is used
with aggregate function like: count, MAX,
MIN etc.

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for Group by statement


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for Having Clause


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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
KIXVJDEPYZ order by sorts the data into ascending

order

 And to sort the data into descending order need to

use desc keyword

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for Order by statement


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Subquery in SQL
Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Inner Query Outer Query

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Output of inner query added to the outer query

Outer query will get executed to get the final

result

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

 Correlated subquery: A correlated subquery reads every row

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

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

Demo for Subqueries


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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
[email protected] complex queries faster
KIXVJDEPYZ 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
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

SQL with Python


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ
High level Interpreted

Object oriented
Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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


KIXVJDEPYZ
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
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

5 Graphical user interface


4

2 Length of the program is


Mathematical computation
short
can be done easily
Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
[email protected] prohibited.
Proprietary content. ©Great Learning. All Reserved. Unauthorized use or distribution prohibited
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
KIXVJDEPYZ 4
Frame work

5 Supports Big Data, Machine Learning 6


Supports Automation
and Cloud computing

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
[email protected] prohibited.
Proprietary content. ©Great Learning. All Reserved. Unauthorized use or distribution prohibited
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]
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

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]
KIXVJDEPYZ

Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
[email protected] prohibited.
Proprietary content. ©Great Learning. All Reserved. Unauthorized use or distribution prohibited
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ

SQL with Python


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
KIXVJDEPYZ
Thank You

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by [email protected] prohibited.

Sharing or publishing the contents in part or full is liable for legal action.

You might also like