KEY ANALYST
SKILLS
TABLE OF
CONTENTS
ANDREW
MADSON
INTRODUCTION
WHAT IS A DATA BASE?
HOW IS IT ORGANIZED?
WHAT IS SQL?
WHAT ARE THE
DIFFERENT SQL
DATABASES?
ANDREW
MADSON
WHAT IS A
DATABASE?
A database is an organized
collection of data that is stored and
accessed electronically. Databases
are designed to hold structured data
or data that can be stored in tables
with rows and columns. They
provide a way to manage large
amounts of data efficiently and
allow for complex operations and
queries to be performed on the data.
ANDREW
MADSON
DATABASE
COMPONENTS
A collection of database objects, including tables, views,
SCHEMA indexes, and procedures.
A collection of related data held in a structured format
TABLE within a database. It consists of columns, and rows.
PRIMARY A column (or a set of columns) in a table that uniquely
identifies each row in that table.
KEY
FOREIGN a column (or a set of columns) in one table, that is used to
link two tables together.
KEY
Similar to an index in a book. It's a pointer to data in a
table. An index in a database is a data structure that
INDEX improves the speed of data retrieval operations on a
database table.
ANDREW
MADSON
HOW IS IT
ORGANIZED?
TABLE
PRIMARY
KEY
FOREIGN
KEY
SCHEMA
ANDREW
MADSON
WHAT IS A
TABLE?
SOURCE: https://www.w3resource.com/sql/sql-basic/the-components-of-a-table.php
ANDREW
MADSON
WHAT IS SQL?
SQL (Structured Query Language) is
a programming language used to
manage and manipulate relational
databases. SQL is primarily used for
managing data held in a relational
database management system
(RDBMS) or for processing data in a
stream management system. It is
particularly effective for handling
structured data, i.e., data
incorporating relations among
entities and variables.
ANDREW
MADSON
WHAT ARE THE TYPES
OF SQL DATABASES?
This is an open-source relational database management system (RDBMS)
owned by Oracle. It's widely used in web applications and is a component
MySQL of the LAMP web application software stack (Linux, Apache, MySQL,
Perl/PHP/Python).
An open-source object-relational database system, PostgreSQL supports
PostgreSQL both SQL (relational) and JSON (non-relational) querying. It's known for its
performance, robustness, and advanced features.
Unlike most other SQL databases, SQLite is a serverless database and is
SQLite embedded into the end program. It's a popular choice for local/client
storage in web browsers and mobile apps.
This is a multi-model database management system produced and
Oracle marketed by Oracle Corporation. It's widely used in enterprise applications.
Developed by Microsoft, SQL Server is a relational database management
SQL Server system that supports a wide range of transaction processing, business
intelligence, and analytics applications in corporate IT environments.
MariaDB is a community-developed, commercially supported fork of the
MySQL relational database management system. It was created by the
MariaDB original developers of MySQL after concerns arose when Oracle acquired
MySQL.
An RDBMS from IBM, DB2 supports the relational model, but also supports
IBM DB2 object-relational features and non-relational structures like JSON and XML.
ANDREW
MADSON
SQL BASICS
SQL SYNTAX
SELECT - FROM - WHERE
GROUP BY - HAVING
ORDER BY
AGGREGATE
FUNCTIONS
ANDREW
MADSON
SELECT - FROM -
WHERE
This command is used to select data from a
database. The data returned is stored in a
result table, called the result-set. You
SELECT specify the columns you want after the
SELECT keyword. If you want to select all
columns, you can use *.
This keyword is used to specify the table
from which to select the data. You write the
FROM name of the table (or tables) after the
FROM keyword.
This keyword is used to filter the results.
After the WHERE keyword, you specify the
WHERE conditions that the data must meet. You
can use operators like =, <, >, <=, >=, <> (not
equal), BETWEEN, LIKE, and IN.
ANDREW
MADSON
EXAMPLE: SELECT -
FROM - WHERE
n this example, the SELECT command is
SELECT used to specify that we want the FirstName
and LastName columns.
The FROM command is used to specify that
FROM we're selecting from the Employees table.
The WHERE clause is used to filter out any
employees whose Salary is not greater than
WHERE 50000. So the statement will return a table
of first and last names of employees who
earn more than 50000.
ANDREW
MADSON
GROUP BY - HAVING
The GROUP BY statement is often used
GROUP with aggregate functions (COUNT, MAX,
MIN, SUM, AVG) to group the result-set by
BY one or more columns.
The HAVING clause was added to SQL
because the WHERE keyword could not be
used with aggregate functions. It is used to
HAVING filter the results of a grouping. It acts like a
WHERE clause but for groups of rows
rather than individual rows.
ANDREW
MADSON
EXAMPLE: GROUP BY
In this example, the SELECT statement is
combined with GROUP BY to group the
employees based on their Department and
SELECT then count the number of EmployeeID in each
department. The COUNT(EmployeeID)
function will return the number of employees
in each department.
The HAVING clause is then used to filter out
any departments where the count of
EmployeeID (which is given the alias
HAVING NumOfEmployees) is not greater than 5. So the
statement will return a table of departments
and their employee counts, but only for
departments with more than 5 employees.
ANDREW
MADSON
ORDER BY
The ORDER BY keyword in SQL is
used to sort the result-set in
ORDER BY ascending or descending order.
It sorts the records in a result
set by one or more columns.
By default, ORDER BY sorts the
data in ascending order. If you
want to specify sorting in
ASCENDING ascending order, you can use
the ASC keyword, but it's not
required.
If you want to sort the data in
DESCENDING descending order, you can use
the DESC keyword.
ANDREW
MADSON
EXAMPLE: ORDER BY
In this example, the SELECT statement is used
to specify that we want the FirstName,
SELECT LastName, and Salary columns from the
Employees table.
The ORDER BY keyword is used to sort the
ORDER
results by the Salary column in descending
order (DESC). So the statement will return a
table of first names, last names, and salaries of
BY employees, ordered from the highest salary to
the lowest.
ANDREW
MADSON
AGGREGATE
FUNCTIONS
This function returns the
COUNT number of rows that matches a
specified criterion.
This function returns the
AVG average value of a numeric
column.
This function returns the
MIN smallest value of the selected
column.
This function returns the largest
MAX value of the selected column.
This function returns the total
SUM sum of a numeric column.
ANDREW
MADSON
EXAMPLE:
AGGREGATE
FUNCTIONS
EmployeeID FirstName LastName Department Salary
1 John Doe HR 70000
2 Jane Smith IT 80000
3 Mary Johnson HR 90000
4 James Brown IT 70000
5 Emily Davis Marketing 80000
6 Robert Miller HR 80000
7 Michael Wilson Marketing 70000
ANDREW
MADSON
EXAMPLE: COUNT
TotalEmployees
OUTPUT 7
This statement returns the total
EXPLANATION number of employees
(EmployeeID).
ANDREW
MADSON
EXAMPLE: AVG
AverageSalary
OUTPUT 77142.86
This SQL statement calculates
EXPLANATION and returns the average salary
(Salary) of all employees.
ANDREW
MADSON
EXAMPLE: MIN
LowestSalary
OUTPUT 70000
This SQL statement finds and
EXPLANATION returns the lowest salary (Salary)
from all employees.
ANDREW
MADSON
EXAMPLE: MAX
HighestSalary
OUTPUT 90000
This SQL statement finds and
EXPLANATION returns the highest salary
(Salary) from all employees.
ANDREW
MADSON
EXAMPLE: SUM
TotalSalary
OUTPUT 540000
This SQL statement calculates
and returns the total sum of
EXPLANATION salaries (Salary) for all
employees.
ANDREW
MADSON
JOINS
This returns records that have
INNER matching values in both tables.
This returns all records from the
left table, and the matched
LEFT records from the right table. If
there is no match, the result is
NULL on the right side.
This returns all records from the
right table, and the matched
RIGHT records from the left table. If
there is no match, the result is
NULL on the left side.
This returns all records when
there is a match in either the
FULL left or the right table. If there is
no match, the result is NULL on
either side.
ANDREW
MADSON
EXAMPLE: JOINS
TABLE A 'EMPLOYEES'
EmployeeID FirstName LastName
1 John Doe
2 Jane Smith
3 Mary Johnson
TABLE B 'DEPARTMENTS'
DeptID DeptName EmployeeID
1 HR 1
2 IT 2
3 Marketing 4
ANDREW
MADSON
EXAMPLE: INNER
TABLE A 'EMPLOYEES' TABLE B 'DEPARTMENTS'
EmployeeID FirstName LastName DeptID DeptName EmployeeID
1 John Doe 1 HR 1
2 Jane Smith 2 IT 2
3 Mary Johnson 3 Marketing 4
OUTPUT
FirstName LastName DeptName
John Doe HR
Jane Smith IT
ANDREW
MADSON
EXAMPLE: LEFT
TABLE A 'EMPLOYEES' TABLE B 'DEPARTMENTS'
EmployeeID FirstName LastName DeptID DeptName EmployeeID
1 John Doe 1 HR 1
2 Jane Smith 2 IT 2
3 Mary Johnson 3 Marketing 4
OUTPUT
FirstName LastName DeptName
John Doe HR
Jane Smith IT
Mary Johnson NULL
ANDREW
MADSON
EXAMPLE: RIGHT
TABLE A 'EMPLOYEES' TABLE B 'DEPARTMENTS'
EmployeeID FirstName LastName DeptID DeptName EmployeeID
1 John Doe 1 HR 1
2 Jane Smith 2 IT 2
3 Mary Johnson 3 Marketing 4
OUTPUT
FirstName LastName DeptName
John Doe HR
Jane Smith IT
NULL NULL Marketing
ANDREW
MADSON
EXAMPLE: FULL
TABLE A 'EMPLOYEES' TABLE B 'DEPARTMENTS'
EmployeeID FirstName LastName DeptID DeptName EmployeeID
1 John Doe 1 HR 1
2 Jane Smith 2 IT 2
3 Mary Johnson 3 Marketing 4
OUTPUT
FirstName LastName DeptName
John Doe HR
Jane Smith IT
Mary Johnson NULL
NULL NULL Marketing
ANDREW
MADSON
RESOURCES
SQL: A Practical Introduction for
Querying Databases
Introduction to SQL on DataCamp
The Complete SQL Bootcamp 2021:
Go from Zero to Hero on Udemy
Intro to SQL: Querying and managing
data on Khan Academy
Learn SQL on Codecademy
ANDREW
MADSON
HOORAY!
🥳
Save this post, and tag me
as you develop these data
analytics core skills.
HAPPY LEARNING!
🙌
ANDREW
MADSON