0% found this document useful (0 votes)
14 views17 pages

Session One Presentation

Uploaded by

Mastan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

Session One Presentation

Uploaded by

Mastan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Deep-Dive Into SQL – Session One

.education
Session Content

• What is SQL?
• Connecting to the course database
• Selecting data from a table
• Filtering rows
• Logical and mathematical operators
• Some notes on syntax, structure,
terminology, and dialects
What is SQL?

• SQL is a language designed for managing relational databases

• These are made up of tables, rows, and columns

• You can consult this structure in the database schema

• Why use databases for storage?

• Speed, size, scalability, concurrency, relationality

• SQL allows us to retrieve data to then visualize, model, and analyse

• Its important that you know how to retrieve data ‘smartly’


Connecting to the course database

• During the course we will enable you to practice on real-life


databases by hosting some on our servers

• To access these, we recommend DBeaver.

• To connect, enter the following details:


• host: sql.wdss.io
• port: 33061
• user: guest
• password: relational
An example relational database
Fetching data by columns
• The simplest selection of data that we can do is
retrieving specific columns from a specific table:

• SELECT column_name
FROM table_name;

• You can also select multiple columns in one go:


• SELECT column_x, column_y, column_z
FROM table_a;
Fetching data by columns
• You might not know which columns you need to use in your
analysis, in which case you can select all columns in a table:

• SELECT *
FROM table_a;

• Some datasets have ugly column names, fix this by


assigning an alias to a column when selecting:

• SELECT ho#r%iblena8e AS new_column_name


FROM table_a;
Class Exercise 1
Filtering rows with the WHERE clause
• The next condition we might wish to impose on the
data we fetch is one that filters the rows selected:

• SELECT column_a, column_b


FROM table_name
WHERE column_a = x;

• Too many results? Add a LIMIT to your queries:

• SELECT column_a, column_b


FROM table_name
WHERE column_a = x
LIMIT 10;
Logical operators
• The AND keyword separates conditions when BOTH
should be satisfied:

• WHERE (column_a = x) AND (column_b=y)

• The OR keyword separates conditions when either one


needs to be satisfied:

• WHERE (column_a = x) OR (column_b=y)

• The NOT keyword is used to negate a condition:

• WHERE (column_a = x) AND NOT (column_b=y)


Mathematical operators
• Similarly, you can use mathematical operators to filter rows

• The ‘>’ and ‘<’ operators are used for strict inequalities:

• WHERE numerical_column_a > 4

• The ‘>=’ and ‘<=’ operators are used for non-strict inequalities:

• WHERE numerical_column_a >= 4

• The ‘<>’ operator is used for ‘not equals’:

• WHERE numerical_column_a <> 4


Class Exercise 2
Some notes on syntax and structure
• Queries have a LEGO-like structure: they start with broad
selections of data and refine this selection more with each clause

• It is convention to capitalize SQL keywords:


• SELECT, FROM, WHERE, AND, OR

• Queries always end with a semicolon

• Be careful with using single quote marks for string columns


• WHERE population > 30000 v.s WHERE country = ‘Germany’
Some notes on terminology
• SQL has its own terminology, and its good to know it:
• Statements are instructions.
• Queries are a type of statement.
• Clauses are the LEGO brick questions.
• Predicates are true/false answers to clauses.
• Expressions are scalars or tables
Some notes on dialects
• SQL is a language. And languages have variants:
• Microsoft Server SQL
• Oracle Database
• PostgreSQL
• MySQL (the one taught in this course)

• There may be syntax discrepancies amongst variants


• For example, the LIMIT clause in MSSQL Server:

• SELECT TOP number column_name


FROM table_name
WHERE condition;
Class Exercise 3
End.
Thanks for coming!

You might also like