0% found this document useful (0 votes)
27 views

SQL_Skills_Development_Session_Introduction_to_query

Uploaded by

gopalms066
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)
27 views

SQL_Skills_Development_Session_Introduction_to_query

Uploaded by

gopalms066
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/ 25

SQL Skills

Development
Session
Introducing queries

Alisa Hudozhnyk
2024
© 2024 Jones Lang LaSalle IP, Inc. All rights reserved.
books
Why is SQL useful for?
id title author genre pub_year
patrons
Atul Non-
638 Being Mortal 2015
card_num name member_year total_fine Gawande fiction
Tara Non-
912 Educated 2018
54378 Izzy 2012 9.86 Westover fiction

94722 Maham 2020 0 Elie Non-


322 Night 1956
Wiesel fiction
45783 Jasmin 2022 2.05 Where the
Maurice
90123 James 1989 0 156 Wild Things Childrens 1963
Sendak
Are
checkouts

id start_date due_date card_num book_id

567 Being Mortal 2024-05-27 54378 638

568 Educated 2024-06-24 54378 322

569 Night 2024-07-11 45783 156

Where the
570 Wild Things 2024-08-28 90123 912
Are
2 |
Best for large datasets How did website
traffic change
when a feature
was introduced?

Which products get


Which product had the worst review
the highest sales scores from
last week? customers?

3 |
Keywords
Keywords are reserved words for operations. Common keywords: SELECT, FROM

SELECT name FROM patrons

patrons
patrons
card_num name member_year total_fine card_num INT
54378 Izzy 2012 9.86 name VARCHAR
94722 Maham 2020 0 member_year INT
45783 Jasmin 2022 2.05 total_fine NUMERIC
90123 James 1989 0 books
checkouts
id INT
id INT
title VARCHAR
start_date DATE
author VARCHAR
due_date DATE
genre VARCHAR
4 |
card_num INT
pub_year INT
Our first query
SELECT name
FROM patrons;

patrons
card_num name member_year total_fine
name
54378 Izzy 2012 9.86

Izzy 94722 Maham 2020 0


45783 Jasmin 2022 2.05
Maham
90123 James 1989 0
Jasmin
James

Query results often called result set

5 |
Selecting multiple fields
SELECT card_num, name SELECT name, card_num
FROM patrons; FROM patrons;

card_num name name card_num

54378 Izzy Izzy 54378


94722 Maham Maham 94722
45783 Jasmin Jasmin 45783
90123 James James 90123

6 |
Selecting multiple fields
SELECT card_num, name, total_fine
FROM patrons;

card_num name total_fine

54378 Izzy 9.86


94722 Maham 0
45783 Jasmin 2.05
90123 James 0

7 |
Selecting all fields
SELECT *
FROM patrons;

card_num name member_year total_fine

54378 Izzy 2012 9.86


94722 Maham 2020 0
45783 Jasmin 2022 2.05
90123 James 1989 0

8 |
Answer the question

Which of the below scenarios describes a situation


in which using SQL would be useful?

a) All data needed to answer the business question is presented in a spreadsheet, and no
complicated relationship exist between different data points.

b) Large amounts of data about many different but related areas of a business are housed in
a relational database.

c) The data needed to answer the business question doesn’t exist yet.

9 |
Developing SQL style
SELECT CARD_NUM, TOTAL_FINE
from patrons

Make CARD_NUM and TOTAL_FINE lowercase All code should be on just one line

Capitalize from Capitalize patrons

Add a ; at the end of the query Make SELECT lowercase

10 |
Querying Property Hub tables

11 |
Three-part naming convention
SELECT OneViewClientID
FROM [DW_All_Clients].[PRPTY].[vw_WH_OneView_Client_Property_All];

the name of the database the name of the view being queried

the schema name within that database

In SQL, this three-part naming convention (database.schema.object) is used to fully qualify and precisely
identify the data source.

12 |
Three-part naming convention
SELECT OneViewClientID
FROM [DW_All_Clients].[PRPTY].[vw_WH_OneView_Client_Property_All];

the name of the database the name of the view being queried

the schema name within that database

Database: This is the top-level container that holds all the data for a specific application or set of related applications.
It's like a big filing cabinet.

Schema: This is a way to organize objects within a database. It's like a drawer in the filing cabinet. A database can
have multiple schemas to group related tables, views, and other objects.

View: This is a virtual table based on the result of a SQL query. It doesn't store data itself but presents data from
one or more tables in a specific way. It's like a specialized report pulled from your filing cabinet.

13 |
Square brackets [ ]
SELECT OneViewClientID
FROM [DW_All_Clients].[PRPTY].[vw_WH_OneView_Client_Property_All];

The use of square brackets [ ] in SQL is not always necessary. Here's when they are used and
when they can be omitted:

1. Square brackets are typically used when:


• The object name contains spaces
• The object name contains special characters
• The object name is a reserved keyword in SQL
• You want to ensure case sensitivity (in some database systems)

2. If the object names (database, schema, table, column) don't have spaces, special characters, or aren't reserved
keywords, you can often omit the square brackets.

14 |
Square brackets [ ]
SELECT OneViewClientID
FROM [DW_All_Clients].[PRPTY].[vw_WH_OneView_Client_Property_All];

So, the query could potentially be written as:


SELECT OneViewClientID
FROM DW_All_Clients.PRPTY.vw_WH_OneView_Client_Property_All;

However, using square brackets is often considered a best practice because:


•It clearly delineates object names
•It prevents potential errors if object names change in the future
•It maintains consistency in coding style
•In practice, many developers use square brackets consistently for all object names to avoid any potential issues and
maintain readability, even when not strictly necessary.

15 |
Querying Property Hub data

Use SQL to return a result set of all OneViewClientID included in


the specified view.

16 |
Querying Property Hub data

SELECT both the OneViewClientID and Address_1 fields from


vw_WH_OneView_Client_Property_All

17 |
Querying Property Hub data

SELECT all fields from the


vw_WH_OneView_Client_Property_All view

18 |
Writing queries: Aliasing
Use aliasing to rename columns

19 |
Writing queries: Selecting DISTINCT records
SELECT year_hired SELECT DISTINCT year_hired
FROM employees; FROM employees;

year_hires year_hires

2020 2020
2017 2017
2022 2022
2021 2021
2020
2021

20 |
Writing queries: DISTINCT with multiple fields
SELECT dept_id, year_hired
FROM employees;
employees
year_
id name dept_id job_level_id
hired dept_id year_hires
54378 Darius 1 3 2020
94722 Raven 2 3 2017 1 2020
45783 Eduardo 2 1 2022 2 2017
90123 Maggie 3 2 2011
2 2022
67284 Amy 2 2 2009
3 2021
26148 Meehir 3 3 2021
2 2020
3 2021

21 |
Writing queries: DISTINCT with multiple fields
SELECT DISTINCT dept_id, year_hired
FROM employees;

dept_id year_hires

1 2020
2 2017
2 2022
3 2021
2 2020

22 |
SQL flavours
SQL has a few different versions, or flavours. Some are free, while others have customer
support and are made to complement major databases such as Microsoft’s SQL Server or Oracle
Databases, which are used by many companies.

All SQL flavours are used with table-based relational databases like the ones we’ve seen, and
most keywords are shared between them. In fact, all SQL flavours must follow universal standards
set by the International Organization for Standards and the American National Standards Institute.
Only additional features on top of these standards result in different SQL flavours.

▪ Both free and paid


▪ All used with relational databases
▪ Vast majority of keywords are the same
▪ All must follow universal standards
▪ Only the additions on top of these standards make flavours different

23 |
Comparing PostgreSQL and SQL Server
Like dialects of the same language
PostgreSQL: SQL Server:

SELECT id, name


SELECT TOP(2) id, name
FROM employees
FROM employees;
LIMIT 2;

id name id name

54378 Darius 54378 Darius


94722 Raven 94722 Raven

24 |
Thank You for your attention!
Reach out with questions or ideas as you begin your SQL journej!

25 |

You might also like