SQL_Skills_Development_Session_Introduction_to_query
SQL_Skills_Development_Session_Introduction_to_query
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
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?
3 |
Keywords
Keywords are reserved words for operations. Common keywords: SELECT, FROM
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
5 |
Selecting multiple fields
SELECT card_num, name SELECT name, card_num
FROM patrons; FROM patrons;
6 |
Selecting multiple fields
SELECT card_num, name, total_fine
FROM patrons;
7 |
Selecting all fields
SELECT *
FROM patrons;
8 |
Answer the question
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
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
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
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:
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];
15 |
Querying Property Hub data
16 |
Querying Property Hub data
17 |
Querying Property Hub data
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.
23 |
Comparing PostgreSQL and SQL Server
Like dialects of the same language
PostgreSQL: SQL Server:
id name id name
24 |
Thank You for your attention!
Reach out with questions or ideas as you begin your SQL journej!
25 |