[go: up one dir, main page]

0% found this document useful (0 votes)
25 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)
25 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 weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Joins In SQL
Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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
weslian1223@gmail.com
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 weslian1223@gmail.com prohibited.

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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for Inner join In SQL


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for LEFT join In SQL


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for RIGHT join In SQL


Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for FULL join In SQL


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

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

A self helps to join with itself

weslian1223@gmail.com
KIXVJDEPYZ

Table join with itself

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for SELF join In SQL


Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
weslian1223@gmail.com 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.
weslian1223@gmail.com
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 weslian1223@gmail.com 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
weslian1223@gmail.com
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 weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for Group by statement


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for Having Clause


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
 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 weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for Order by statement


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Subquery in SQL
Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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?
weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
How many parts are there in a
subquery?

Subquery

weslian1223@gmail.com
KIXVJDEPYZ

Inner Query Outer Query

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
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 weslian1223@gmail.com 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


weslian1223@gmail.com
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 weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

Demo for Subqueries


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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
weslian1223@gmail.com 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 weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ

SQL with Python


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ
High level Interpreted

Object oriented
Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com • 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 weslian1223@gmail.com 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
weslian1223@gmail.com
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.
weslian1223@gmail.com 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


weslian1223@gmail.com 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 weslian1223@gmail.com 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/

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com 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

weslian1223@gmail.com
KIXVJDEPYZ

Proprietary
Thiscontent.
file © Great Learning.
is meant All Rights
for personal useReserved.
byRightsUnauthorized use or distributiononly.
weslian1223@gmail.com 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.
weslian1223@gmail.com
KIXVJDEPYZ

SQL with Python


Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

Sharing or publishing the contents in part or full is liable for legal action.
weslian1223@gmail.com
KIXVJDEPYZ
Thank You

Proprietary
Thiscontent. © Great Learning.
file is meant All Rights
for personal useReserved. Unauthorized use or distributiononly.
by weslian1223@gmail.com prohibited.

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

You might also like