SQL Crash Course
SQL Crash Course
SQL Crash Course
SQL Crash
Course
What Is a Database?
Let’s start with the basics. A database is a place to store
data in an organized way. There are many ways to organize
data, and as a result, there are many databases to choose
from. The two categories that databases fall into are SQL
and NoSQL.
SQL
SQL is short for Structured Query Language. Imagine you
have an app that remembers all of your friend’s birthdays.
SQL is the most popular language you would use to talk to
that app.
English: “Hey app. When is my husband’s birthday?”
SQL: SELECT * FROM birthdays
WHERE person = 'husband';
SQL databases are often called relational databases
because they are made up of relations, which are more
commonly referred to as tables. Many tables connected to
each other make up a database. Figure 1-1 shows a picture
of a relation in a SQL database.
Figure 1-1. A relation (also known as a table) in a SQL database
NoSQL
NoSQL stands for not only SQL. It will not be covered in
detail in this book, but I wanted to point it out because the
term has grown a lot in popularity since the 2010s and it’s
important to understand there are ways to store data
beyond just tables.
NoSQL databases are often referred to as non-relational
databases, and they come in all shapes and sizes. Their
main characteristics are that they have dynamic schemas
(meaning the schema doesn’t have to be locked in up front)
and they allow for horizontal scaling (meaning the data can
spread across multiple machines).
The most popular NoSQL database is MongoDB, which is
more specifically a document database. Figure 1-2 shows a
picture of how data is stored in MongoDB. You’ll notice that
the data is no longer in a structured table and the number
of fields (similar to a column) varies for each document
(similar to a row).
Oracle Database
SELECT * FROM birthdays WHERE ROWNUM <= 10;
GOOGLING SQL SYNTAX
When searching for SQL syntax online, always include the RDBMS you are
working with in the search. When I first learned SQL, I could not for the
life of me figure out why my copy-pasted code from the internet didn’t
work and this was the reason!
Do this.
Search: create table datetime postgresql
→ Result: timestamp
Search: create table datetime microsoft sql server
→ Result: datetime
Not this.
Search: create table datetime
→ Result: syntax could be for any RDBMS
This book covers SQL basics along with the nuances of five
popular database management systems: Microsoft SQL
Server, MySQL, Oracle Database, PostgreSQL and SQLite.
Some are proprietary, meaning they are owned by a
company and cost money to use, and others are open
source, meaning they are free for anyone to use. Table 1-1
details the differences between the RDBMSs.
NOTE
Going forward in this book:
Microsoft SQL Server will be referred to as SQL Server.
A SQL Query
A common acronym in the SQL world is CRUD, which
stands for “Create, Read, Update, and Delete.” These are
the four major operations that are available within a
database.
SQL Statements
People who have read and write access to a database are
able to perform all four operations. They can create and
delete tables, update data in tables, and read data from
tables. In other words, they have all the power.
They write SQL statements, which is general SQL code that
can be written to perform any of the CRUD operations.
These people often have titles like database administrator
(DBA) or database engineer.
SQL Queries
People who have read access to a database are only able to
perform the read operation, meaning they can look at data
in tables.
They write SQL queries, which are a more specific type of
SQL statement. Queries are used for finding and displaying
data, otherwise known as “reading” data. This action is
sometimes referred to as querying tables. These people
often have titles like data analyst or data scientist.
The next two sections are a quick-start guide for writing
SQL queries, since it is the most common type of SQL code
that you’ll see. More details on creating and updating
tables can be found in Chapter 5.
which says, show me all of the data within the table named
my_table—all of the columns and all of the rows.
While SQL is case-insensitive (SELECT and select are
equivalent), you’ll notice that some words are in all caps
and others are not.
The uppercase words in the query are called keywords,
meaning that SQL has reserved them to perform some
sort of operation on the data.
SELECT *
FROM my_table
WHERE column1 > 100
ORDER BY column2;
NOTE
The -- is the start of a comment in SQL, meaning the text after it is
just for documentation’s sake and the code will not be executed.
For the most part, the SELECT and FROM clauses are required and all
other clauses are optional. The exception is if you are selecting a
particular database function, then only the SELECT is required.
Order of Execution
The order that SQL code is executed is not something
typically taught in a beginner SQL course, but I’m
including it here because it’s a common question I received
when I taught SQL to students coming from a Python
coding background.
A sensible assumption would be that the order that you
write the clauses is the same order that the computer
executes the clauses, but that is not the case. After a query
is run, this is the order that the computer works through
the data:
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY
A Data Model
I’d like to spend the final section of the crash course going
over a simple data model and point out some terms that
you’ll often hear in fun SQL conversations around the
office.
A data model is a visualization that summarizes how all of
the tables in a database are related to one another, along
with some details about each table. Figure 1-3 is a simple
data model of a student grades database.
Table 1-2 lists out the technical terms that describe what’s
happening in the data model.
D A database is a place to store data in This data model shows all of the
a an organized way. data in the student grades
t database.
a
b
a
s
e
K
e
y
R A relationship describes how the In this data model, the two tables
e rows in one table map to the rows in have a one-to-many relationship
l another table. In a data model, it is represented by the fork. One
a represented by a line with symbols student can have many grades, or
t at the end points. Common types are one row of the Students table
i one-to-one and one-to-many maps to multiple rows in the
o relationships. Grades table.
n
s
h
i
p