You are on page 1of 42

SQL Tutorial

Sampriti Chatterjee (Great Learning)


Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Alias in SQL

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

table

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Joins In SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
What is join in SQL?

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

based on a condition

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Types of join in SQL

INNER JOIN

01
Types of join in SQL LEFT JOIN
02

SELF JOIN 05 03 RIGHT JOIN


04
FULL JOIN

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Types of join in SQL?

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
INNER JOIN

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

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for Inner join In SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
LEFT JOIN

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

from the right table

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for LEFT join In SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
RIGHT JOIN

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

from the left table

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for RIGHT join In SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
FULL JOIN

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

a match or not

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for FULL join In SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
SELF JOIN

A self helps to join with itself

Table join with itself

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for SELF join In SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Group by and Having concept in SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Group By

Group by statement helps to groups rows which has similar properties

Most of the time group by function is used


with aggregate function like: count, MAX,
MIN etc.

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for Group by statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
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

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for Having Clause
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Order By

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

order

 By default order by sorts the data into ascending

order

 And to sort the data into descending order need to

use desc keyword

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for Order by statement
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Subquery in SQL
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How to handle two or
more queries at a
time?

Do I need to write
two separate query?

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
What is subquery?

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

query inside

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How many parts are there in a
subquery?

Subquery

Inner Query Outer Query

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How subquery executes?

First inner query get executed

Get the result from inner query

Output of inner query added to the outer query

Outer query will get executed to get the final

result

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Types of subquery

 Nested subquery: Nested subquery first executes the

inner SELECT query and then with the returning values executes

the outer query

 Correlated subquery: A correlated subquery reads every row

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

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Demo for Subqueries
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
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 queries faster
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 content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
SQL with Python
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Introduction to Python

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

High level Interpreted

Object oriented
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
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
• He needed a short, unique, and slightly
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 content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Why should you learn Python?

Python is simple and beginner


3 friendly language

1 Web development using


Python

5 Graphical user interface


4

2 Length of the program is


Mathematical computation
short
can be done easily
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Why Python is so popular?

1 Largest community for Learners and Collaborators 2 Open source

Easy to learn and usable flexibility Huge numbers of Python libraries and
3 4
Frame work

5 Supports Big Data, Machine Learning 6


Supports Automation
and Cloud computing

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Installing Python

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

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Popular IDE for Python: Pycharm

Site to install Python ->


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

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Popular IDE for Python: Anaconda

Anaconda installation site->


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

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Popular IDE for Python: Google colab

Google collaboratory link->


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

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
SQL with Python
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Thank You

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.

You might also like