SlideShare a Scribd company logo
Getting Started
With SQL
Relational Database
Overview
● History
● Concept
● Existing technologies
Basic entities in relational databases
ID Column1 Column2 Column3
1 datum1.
1
datum2.
1
datum3.
1
2 datum1.
2
datum2.
2
datum3.
2
3 datum1.
3
datum2.
3
datum3.
3
4 datum1.
4
datum2.
4
datum3.
4
Tables
● Int
● Varchar(size)
● Date
● Double
● Datetime
● And many
more
Data Types
● One → One
● One → Many
● Many → Many
● Self Referencing
Connections
DB Installation
Steps to install the DB
● Step 1: enter this site postgres
● Step 2: choose the Os you are working on
● Step 3: click on download the installer under Interactive installer by EnterpriseDB
● Step 4: download the dmg file (mac) exe file (windows) of version 10
● Step 5: follow the installer when it asks you for password choose something you can
remember. IF YOU FORGET THIS PASSWORD YOU’LL NEED TO REMOVE ALL
THE COMPONENTS FROM YOUR COMPUTER AND IT IS A MESS
● Step 6: open pgadmin (it is already installed during the postgres installation)
pgAdmin - Connecting to a server
21
3
pgAdmin - Creating new DB
1 2 3
pgAdmin - Creating a new table
1 2
Creating a new table - Live example
pgAdmin - Import data from csv
1 2 3
Import data from csv - Live example
SQL (Structured Query Language)
Basic query
select column1, column2, … from tableName
● The * - selects all columns in a table
● The keyword as - used for giving aliases to columns or tables
● The keyword distinct - returns all distinct values of a table
● The keyword top x|percent - returns x or x percent top results
● The keyword limit x - limits the number of results returned
from a query to x
Example
select id, fName as first_name, lName, age, from person
id fName lName age Country City Phone_Number
1 Sammy Blumberg 43 Israel Jerusalem +972528534467
2 Diana Smith 60 Italy Rome +390611532846
3 Rachel Wilson 24 France Paris +33523449285
Result
id first_name lName age
1 Sammy Blumberg 43
2 Diana Smith 60
3 Rachel Wilson 24
select id, fName as first_name, lName, age, from person
Order Results
select column1, column2, … from tableName
order by column1, column2, …
● Numbers instead of column names
● The keywords asc (default) and desc
Example
select id,fName, lName, age, from person
order by Age
id fName lName age
1 Sammy Blumberg 43
2 Diana Smith 60
3 Rachel Wilson 24
id fName lName age
3 Rachel Wilson 24
1 Sammy Blumberg 43
2 Diana Smith 60
Conditions
select column1, column2, … from tableName
where (not) condition1 (andor + (not)) condition2 ...
● Conditions with operators - =, >, <, >=, <=, <>
○ Name = ‘Rachel’
○ Age >= 23
● Conditions with like keyword - coulmn(x) like pattern
○ lName like ‘%berg’ - any name that ends with berg
● The in keyword - columnX in (dataset)
○ Age in (23,43,67,80,92,13,15,6)
● The between keyword - columnX between a and b
○ Age between 12 and 30
Operators
● = - equals
● <> (!=) - not equals
● > - greater thane
● < - less than
● >= - greater than equals
● <= less than equals
Wildcards
● 'a%' - Finds any values that starts with "a"
● '%a' - Finds any values that ends with "a"
● '%or%’ - Finds any values that have "or" in any position
● '_r%' - Finds any values that have "r" in the second position
● 'a_%_%' - Finds any values that starts with "a" and are at least 3 characters in length
● 'a%o' - Finds any values that starts with "a" and ends with "o"
● [charlist] - Defines sets and ranges of characters to match
○ [abs]
○ [a-d]
● [^charlist] or [!charlist] - Defines sets and ranges of characters NOT to match
○ [!bef]
○ [!g-r]
Example
select fName from person
where lName = ‘Wilson’ and age > 30
id fName lName age
1 Sammy Blumberg 43
2 Diana Smith 60
3 Rachel Wilson 24
4 Tomy Wilson 32
fName
Tomy
Group by + Conditions
select column1, column2, … from tableName
group by coulmn1, column2
having (not) condition1 (andor + (not)) condition2 …
Conditions in having sentence affect only groups and not the entire
dataset. To add condition on the entire dataset use the where
clause.
Example
select fName, lName from person where age > 24
group by country having lName like ‘B%’ order by Age desc
id fName lName age country
1 Sammy Blumberg 43 USA
2 Diana Smith 60 UK
3 Rachel Wilson 24 USA
4 Jacque Brel 30 France
5 Mike Brant 50 USA
6 Claire Bakingham 37 UK
fName lName
Mike Brant
Sammy Blumberg
Claire Bakingham
Jacque Brel
Joining information
select t1.column1, t1.column2, t2.column1, t2.column2, t2.column3...
from t1 [A] (first table)
Join t2 [B] (second table) on t1.columnX = t2.columnX
● (Inner) join - Returns records that have matching values in both tables
● Left join - Return all records from the left table, and the matched records from
the right table
● Right join - Return all records from the right table, and the matched records
from the left table
● Full outer join - Return all records when there is a match in either left or right
table
Join Types
Full Outer JoinRight joinLeft joinInner join
TableA TableA TableATableBTableB TableBTableA TableB
Example
select p.fName, p.lName, cd.email
from person p
join contact_details cd p.id = cd.person_id
where fname = ‘Rachel’
id fName lName
1 Sammy Blumberg
2 Diana Smith
3 Rachel Wilson
4 Rachel Rosenberg
id person_id email phone
1 3 rw@gmail.com 0542221307
2 2 ds@gmail.com 0521342555
3 1 sb@gmail.com 0503078242
4 4 rr@gmail.com 0545557731
Result
select p.fName, p.lName, cd.email
from person p
join contact_details cd p.id = cd.person_id
where fname = ‘Rachel’
fName lName email
Rachel Wilson rw@gmail.com
Rachel Rosenberg rr@gmail.com
Subqueries
● Each query returns a table
● Each query manipulates the data in one or more tables
● Hence we can run queries on results of other queries
● These “other queries” are called subqueries
Example
select p.fName as first_name, p.eyesColor as eyes_color
from person p
where p.eye_color in (select color from colors where
colorTag = ‘eyes’)
id fName eyesColor
1 Sammy blue
2 Diana green
3 Rachel blue
4 Danielle brown
id color colorTag code
1 blue eyes #13c3ff
2 green eyes #32c512
3 brown hair #673a07
4 black skin #000000
Result
id first_name eyesColor
1 Sammy blue
2 Diana green
3 Rachel blue
select p.fName as first_name, p.eyesColor as eyes_color
from person p
where p.eye_color in (select color from colors where
colorTag = ‘eyes’)
Functions
Select [columns…], function(columnX), [columns…] from
tableName
● Count() - counts the number of rows that returned from the query.
Doesn’t count nulls
● Max() - gives the maximal value in a column
● Min() - gives the minimum value in a column
● Avg() - gives the average value of all values in a column
● Sum() - sums up all values in a column
● More...
Example
select Count(*) as num_of_people, Avg(age) as average_age from person
where country = ‘USA’
id fName lName age country
1 Sammy Blumberg 43 USA
2 Diana Smith 60 UK
3 Rachel Wilson 24 USA
4 Jacque Brel 30 France
5 Mike Brant 50 USA
6 Claire Bakingham 37 UK
num_of_people average_age
3 51
Demo
SQL with PostgreSQL - Getting Started

More Related Content

PPTX
9.1 and 9.2
nscross40
 
PPTX
Structure Query Language Advance Training
parisaxena1418
 
DOC
Learn sql queries
Sanjay Mago
 
PPTX
Introduction to SQL
Mahir Haque
 
PPT
Higher SQL
missstevenson01
 
PPTX
vnd.openxmlformats-officedocument.presentationml.presentation&rendition=1.pptx
jaijsr12345
 
PPTX
Sql slid
pacatarpit
 
PPT
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
9.1 and 9.2
nscross40
 
Structure Query Language Advance Training
parisaxena1418
 
Learn sql queries
Sanjay Mago
 
Introduction to SQL
Mahir Haque
 
Higher SQL
missstevenson01
 
vnd.openxmlformats-officedocument.presentationml.presentation&rendition=1.pptx
jaijsr12345
 
Sql slid
pacatarpit
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 

Similar to SQL with PostgreSQL - Getting Started (20)

PDF
Learning SQL
DAVID RAUDALES
 
PDF
Learning SQL
David Raudales
 
DOCX
SQL report
Ahmad Zahid
 
DOC
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
PPTX
Server Query Language – Getting Started.pptx
auzee32
 
PPT
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
PPTX
rdbms parul university oracle dbms bca mca
VaibhavSrivastav52
 
PPT
MY SQL
sundar
 
DOCX
Sql
Archana Rout
 
PPT
SQL.ppt
Ranjit273515
 
PPTX
SQL Server Learning Drive
TechandMate
 
PPT
MS SQL Server 1
Iblesoft
 
PPTX
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
Abhishek590097
 
PDF
Oracle Notes
Abhishek Sharma
 
PPT
SQL || overview and detailed information about Sql
gourav kottawar
 
PPT
Mysql 120831075600-phpapp01
sagaroceanic11
 
PPTX
Dbms sql-final
NV Chandra Sekhar Nittala
 
PDF
Four Basic SQL Programming for Learners
JcGarcia53
 
PDF
4 Basic SQL.pdf SQL is a standard language for storing, manipulating and retr...
firozkhan804877
 
Learning SQL
DAVID RAUDALES
 
Learning SQL
David Raudales
 
SQL report
Ahmad Zahid
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
Server Query Language – Getting Started.pptx
auzee32
 
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
rdbms parul university oracle dbms bca mca
VaibhavSrivastav52
 
MY SQL
sundar
 
SQL.ppt
Ranjit273515
 
SQL Server Learning Drive
TechandMate
 
MS SQL Server 1
Iblesoft
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
Abhishek590097
 
Oracle Notes
Abhishek Sharma
 
SQL || overview and detailed information about Sql
gourav kottawar
 
Mysql 120831075600-phpapp01
sagaroceanic11
 
Four Basic SQL Programming for Learners
JcGarcia53
 
4 Basic SQL.pdf SQL is a standard language for storing, manipulating and retr...
firozkhan804877
 
Ad

Recently uploaded (20)

PPTX
International-health-agency and it's work.pptx
shreehareeshgs
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
abhinavmemories2026
 
PDF
Mastering Financial Analysis Materials.pdf
SalamiAbdullahi
 
PPTX
Complete_STATA_Introduction_Beginner.pptx
mbayekebe
 
PPTX
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
sumitmundhe77
 
PDF
Research about a FoodFolio app for personalized dietary tracking and health o...
AustinLiamAndres
 
PPTX
Major-Components-ofNKJNNKNKNKNKronment.pptx
dushyantsharma1221
 
PDF
Company Presentation pada Perusahaan ADB.pdf
didikfahmi
 
PDF
Data Science Trends & Career Guide---ppt
jisajoy3061
 
PPTX
Global journeys: estimating international migration
Office for National Statistics
 
PDF
Company Profile 2023 PT. ZEKON INDONESIA.pdf
hendranofriadi26
 
PDF
Digital Infrastructure – Powering the Connected Age
Heera Yadav
 
PDF
Chad Readey - An Independent Thinker
Chad Readey
 
PPTX
batch data Retailer Data management Project.pptx
sumitmundhe77
 
PPTX
Machine Learning Solution for Power Grid Cybersecurity with GraphWavelets
Sione Palu
 
PPTX
Understanding Prototyping in Design and Development
SadiaJanjua2
 
PDF
CH2-MODEL-SETUP-v2017.1-JC-APR27-2017.pdf
jcc00023con
 
PPTX
Logistic Regression ml machine learning.pptx
abdullahcocindia
 
PPTX
Introduction to Biostatistics Presentation.pptx
AtemJoshua
 
PPTX
Data Security Breach: Immediate Action Plan
varmabhuvan266
 
International-health-agency and it's work.pptx
shreehareeshgs
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
abhinavmemories2026
 
Mastering Financial Analysis Materials.pdf
SalamiAbdullahi
 
Complete_STATA_Introduction_Beginner.pptx
mbayekebe
 
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
sumitmundhe77
 
Research about a FoodFolio app for personalized dietary tracking and health o...
AustinLiamAndres
 
Major-Components-ofNKJNNKNKNKNKronment.pptx
dushyantsharma1221
 
Company Presentation pada Perusahaan ADB.pdf
didikfahmi
 
Data Science Trends & Career Guide---ppt
jisajoy3061
 
Global journeys: estimating international migration
Office for National Statistics
 
Company Profile 2023 PT. ZEKON INDONESIA.pdf
hendranofriadi26
 
Digital Infrastructure – Powering the Connected Age
Heera Yadav
 
Chad Readey - An Independent Thinker
Chad Readey
 
batch data Retailer Data management Project.pptx
sumitmundhe77
 
Machine Learning Solution for Power Grid Cybersecurity with GraphWavelets
Sione Palu
 
Understanding Prototyping in Design and Development
SadiaJanjua2
 
CH2-MODEL-SETUP-v2017.1-JC-APR27-2017.pdf
jcc00023con
 
Logistic Regression ml machine learning.pptx
abdullahcocindia
 
Introduction to Biostatistics Presentation.pptx
AtemJoshua
 
Data Security Breach: Immediate Action Plan
varmabhuvan266
 
Ad

SQL with PostgreSQL - Getting Started

  • 3. Overview ● History ● Concept ● Existing technologies
  • 4. Basic entities in relational databases ID Column1 Column2 Column3 1 datum1. 1 datum2. 1 datum3. 1 2 datum1. 2 datum2. 2 datum3. 2 3 datum1. 3 datum2. 3 datum3. 3 4 datum1. 4 datum2. 4 datum3. 4 Tables ● Int ● Varchar(size) ● Date ● Double ● Datetime ● And many more Data Types ● One → One ● One → Many ● Many → Many ● Self Referencing Connections
  • 6. Steps to install the DB ● Step 1: enter this site postgres ● Step 2: choose the Os you are working on ● Step 3: click on download the installer under Interactive installer by EnterpriseDB ● Step 4: download the dmg file (mac) exe file (windows) of version 10 ● Step 5: follow the installer when it asks you for password choose something you can remember. IF YOU FORGET THIS PASSWORD YOU’LL NEED TO REMOVE ALL THE COMPONENTS FROM YOUR COMPUTER AND IT IS A MESS ● Step 6: open pgadmin (it is already installed during the postgres installation)
  • 7. pgAdmin - Connecting to a server 21 3
  • 8. pgAdmin - Creating new DB 1 2 3
  • 9. pgAdmin - Creating a new table 1 2
  • 10. Creating a new table - Live example
  • 11. pgAdmin - Import data from csv 1 2 3
  • 12. Import data from csv - Live example
  • 14. Basic query select column1, column2, … from tableName ● The * - selects all columns in a table ● The keyword as - used for giving aliases to columns or tables ● The keyword distinct - returns all distinct values of a table ● The keyword top x|percent - returns x or x percent top results ● The keyword limit x - limits the number of results returned from a query to x
  • 15. Example select id, fName as first_name, lName, age, from person id fName lName age Country City Phone_Number 1 Sammy Blumberg 43 Israel Jerusalem +972528534467 2 Diana Smith 60 Italy Rome +390611532846 3 Rachel Wilson 24 France Paris +33523449285
  • 16. Result id first_name lName age 1 Sammy Blumberg 43 2 Diana Smith 60 3 Rachel Wilson 24 select id, fName as first_name, lName, age, from person
  • 17. Order Results select column1, column2, … from tableName order by column1, column2, … ● Numbers instead of column names ● The keywords asc (default) and desc
  • 18. Example select id,fName, lName, age, from person order by Age id fName lName age 1 Sammy Blumberg 43 2 Diana Smith 60 3 Rachel Wilson 24 id fName lName age 3 Rachel Wilson 24 1 Sammy Blumberg 43 2 Diana Smith 60
  • 19. Conditions select column1, column2, … from tableName where (not) condition1 (andor + (not)) condition2 ... ● Conditions with operators - =, >, <, >=, <=, <> ○ Name = ‘Rachel’ ○ Age >= 23 ● Conditions with like keyword - coulmn(x) like pattern ○ lName like ‘%berg’ - any name that ends with berg ● The in keyword - columnX in (dataset) ○ Age in (23,43,67,80,92,13,15,6) ● The between keyword - columnX between a and b ○ Age between 12 and 30
  • 20. Operators ● = - equals ● <> (!=) - not equals ● > - greater thane ● < - less than ● >= - greater than equals ● <= less than equals
  • 21. Wildcards ● 'a%' - Finds any values that starts with "a" ● '%a' - Finds any values that ends with "a" ● '%or%’ - Finds any values that have "or" in any position ● '_r%' - Finds any values that have "r" in the second position ● 'a_%_%' - Finds any values that starts with "a" and are at least 3 characters in length ● 'a%o' - Finds any values that starts with "a" and ends with "o" ● [charlist] - Defines sets and ranges of characters to match ○ [abs] ○ [a-d] ● [^charlist] or [!charlist] - Defines sets and ranges of characters NOT to match ○ [!bef] ○ [!g-r]
  • 22. Example select fName from person where lName = ‘Wilson’ and age > 30 id fName lName age 1 Sammy Blumberg 43 2 Diana Smith 60 3 Rachel Wilson 24 4 Tomy Wilson 32 fName Tomy
  • 23. Group by + Conditions select column1, column2, … from tableName group by coulmn1, column2 having (not) condition1 (andor + (not)) condition2 … Conditions in having sentence affect only groups and not the entire dataset. To add condition on the entire dataset use the where clause.
  • 24. Example select fName, lName from person where age > 24 group by country having lName like ‘B%’ order by Age desc id fName lName age country 1 Sammy Blumberg 43 USA 2 Diana Smith 60 UK 3 Rachel Wilson 24 USA 4 Jacque Brel 30 France 5 Mike Brant 50 USA 6 Claire Bakingham 37 UK fName lName Mike Brant Sammy Blumberg Claire Bakingham Jacque Brel
  • 25. Joining information select t1.column1, t1.column2, t2.column1, t2.column2, t2.column3... from t1 [A] (first table) Join t2 [B] (second table) on t1.columnX = t2.columnX ● (Inner) join - Returns records that have matching values in both tables ● Left join - Return all records from the left table, and the matched records from the right table ● Right join - Return all records from the right table, and the matched records from the left table ● Full outer join - Return all records when there is a match in either left or right table
  • 26. Join Types Full Outer JoinRight joinLeft joinInner join TableA TableA TableATableBTableB TableBTableA TableB
  • 27. Example select p.fName, p.lName, cd.email from person p join contact_details cd p.id = cd.person_id where fname = ‘Rachel’ id fName lName 1 Sammy Blumberg 2 Diana Smith 3 Rachel Wilson 4 Rachel Rosenberg id person_id email phone 1 3 [email protected] 0542221307 2 2 [email protected] 0521342555 3 1 [email protected] 0503078242 4 4 [email protected] 0545557731
  • 28. Result select p.fName, p.lName, cd.email from person p join contact_details cd p.id = cd.person_id where fname = ‘Rachel’ fName lName email Rachel Wilson [email protected] Rachel Rosenberg [email protected]
  • 29. Subqueries ● Each query returns a table ● Each query manipulates the data in one or more tables ● Hence we can run queries on results of other queries ● These “other queries” are called subqueries
  • 30. Example select p.fName as first_name, p.eyesColor as eyes_color from person p where p.eye_color in (select color from colors where colorTag = ‘eyes’) id fName eyesColor 1 Sammy blue 2 Diana green 3 Rachel blue 4 Danielle brown id color colorTag code 1 blue eyes #13c3ff 2 green eyes #32c512 3 brown hair #673a07 4 black skin #000000
  • 31. Result id first_name eyesColor 1 Sammy blue 2 Diana green 3 Rachel blue select p.fName as first_name, p.eyesColor as eyes_color from person p where p.eye_color in (select color from colors where colorTag = ‘eyes’)
  • 32. Functions Select [columns…], function(columnX), [columns…] from tableName ● Count() - counts the number of rows that returned from the query. Doesn’t count nulls ● Max() - gives the maximal value in a column ● Min() - gives the minimum value in a column ● Avg() - gives the average value of all values in a column ● Sum() - sums up all values in a column ● More...
  • 33. Example select Count(*) as num_of_people, Avg(age) as average_age from person where country = ‘USA’ id fName lName age country 1 Sammy Blumberg 43 USA 2 Diana Smith 60 UK 3 Rachel Wilson 24 USA 4 Jacque Brel 30 France 5 Mike Brant 50 USA 6 Claire Bakingham 37 UK num_of_people average_age 3 51
  • 34. Demo