3. SQL Basics

3. SQL Basics

🎯 Learning Goals

  • Understand that SQL is used to store, retrieve, and modify data in (relational) databases
  • Recognize common data types in SQL and understand when to use each
  • Confidently create tables in a database using SQL
  • Confidently insert and retrieve data using SQL

📗 Technical Vocabulary

  • Database
    • Relational database
  • Query
  • Table
  • Record
  • Field
  • Primary key
  • Data type
🌤️
Warm-Up: The Great Data Debate
Is the word, “data,” singular or plural? Debate (briefly) among your group! Feel free to find resources on the Internet to back up your argument.
Resolution: Although we often informally use the word, “data,” as if it is a singular noun, in academic and scientific settings, it is used in its original plural form. In keeping with that, throughout this course we will treat it as a plural noun.
  • Out: “The data indicates…”
  • In: “The data indicate…”
  • Out: “The data was cleaned.”
  • In: “The data were cleaned.”
  • Out: “This is some great data!”
  • In: “These are some great data!”
If you ever need to refer to a single unit, you can use the singular form of the word, “datum.”

🗄️ What Is SQL and What Do We Use It For?

“SQL” stands for Structured Query Language. It is used to store, retrieve, and modify data, or information, stored in a database. A database is a digital place where data is stored.
You can pronounce “SQL” like the word, “sequel.” Let’s break down its full name, working our way backward:
  • The “L” stands for “Language.”
    • SQL is a computer language. Just like a human language, like English or Tagalog, computer languages have vocabulary (words and phrases) and syntax (rules for how to use words and phrases to convey meaning).
    • Luckily, both the vocabulary and syntax of SQL are way smaller and and more straightforward than those of English, so we’ll be able to pick it up more quickly than a new human language. SQL has keywords, commands, and syntax rules that define how we can use it to direct a computer to take specific actions.
notion image
 
  • The “Q” stands for “Query.”
    • You can think of a query as a question written in SQL so that a computer can understand it. For example, if we have a database of KWK scholars which includes their favorite color, we might ask a question like, “What are all the different favorite colors that KWK scholars have?” Since we’ll write this query in SQL, rather than English, it would look like this:
      • SELECT DISTINCT favorite_color FROM kwk_scholars;
      • The select keyword means that we are trying to pick out some information from the table we’re querying. We’re selecting that information.
      • favorite_color is the information we want to select, and kwk_scholars is the table that it lives in. from is another one of those standard keywords.
      • distinct is a keyword that makes sure we don’t select duplicate values. For example, there might be a lot of KWK scholars whose favorite color is purple. But we don’t want to select the same value hundreds of times. Distinct removes those duplicates from the results.
      • The semi-colon is how we end every SQL query. Think of it like the question mark at the end of a question.
  • The “S” stands for “Structured.” The databases that we interact with through SQL store data in a highly structured way. Data are stored in tables with rows and columns. Here’s an example of how data in our kwk_scholars table might look.
    • Each row, or record, contains information about a single scholar. We can say that each row “represents” one scholar.
    • Each column, or field, stores a specific data type. We’ll talk more about data types later, but for now, notice that there are different kinds of data in the different columns. The age column stores numbers, while the name column stores text.
id
name
age
favorite_color
0001
“alex”
16
“pink”
0002
“betty”
13
“green”
0003
“carla”
15
“yellow”
 

🔢 Creating and Populating a Table

We’ll use Replit to write SQL queries throughout these lessons. Create an account and code along!

Step One

After you create a free account, click the “Create App” button in the top left of your account homepage.
notion image

Step Two

Select SQLite for the project template.
Give your app a title, like “SQL Basics.” You can leave the rest of the options as-is.
After you create the app, you will see a window that houses the AI assistant. We don’t need it for now, so feel free to exit!
notion image

Step Three

Let’s turn off AI Autocomplete for now. While we’re learning the basics, it will be more of a distraction than a help.
In the lower left corner of the screen, click on the AI button and un-select the Enable checkbox.
notion image

Step Four

The repl is pre-populated with a basic query. Take a moment to predict what you think will happen when you hit Run. Then, hit Run!
When you hit run, the Console window opens. This is where you will see the output of the queries you run.
You can see the output of your query here: “Hello, world!” was printed to the screen!
notion image
notion image

Step Five

We can run commands directly in the console. Try writing a similar statement here, like:
select 'Hello from the console!';
Hit return / enter on your keyboard to run the query. What happens?
notion image
notion image

Step Six

Now let’s try updating the command in the main.sql file.
Note: the capitalization of the keyword select doesn’t matter to SQLite. In this camp, we will keep keywords lower case.
select 'Hello from main.sql!';
Hit the Run button. What happens?
We can run commands from either place. We will mostly work within the main.sql file, so that our work is automatically saved. However, running commands directly in the console is a great way to prototype things, so don’t be afraid to use it!
 
notion image
notion image

Step Seven

Now, let’s create our first table! We’ll work in main.sql and create a table to keep track of music albums. Our table will have:
  1. A name: albums
  1. 4 fields:
    1. id, an integer, which also serves as the unique primary key for the table. A primary key is an identifier that is guaranteed to be unique for each row of the table.
    2. title, a text field, which will store the title of the album.
    3. artist, a text field, which will store the name of the artist.
    4. release_date, a date field, which will store the date the album came out.
Notice that for each field, we specify a data type. This lets the database know how to treat the data we store there.
For now, we will focus on the following specific data types:
  • integers, or whole numbers, like 0, 1, 2…
  • text, which stores any length of text, which can include letters, punctuation marks, letters, and emoji
  • dates, which stores dates and times.
Type the SQL statement to create the table into the file and hit Run.
There won’t be any output unless there is an error in your statement. If there is, pause and fix it before moving on.
create table albums ( id integer primary key, title text, artist text, release_date date );
notion image
To the right is an example of what an error might look like. It will show the line number where the error is and what kind of error you have. A syntax error means that there was a typo somewhere; carefully read through your code and see if you misspelled a keyword, data type, field name, or missed a semicolon.
notion image

Step Eight

Now, let’s add some data. We’ll use an insert statement to insert our first record into our albums table.
Then, we’ll use a select query to select the data in the table and output it so we can see it.
When we use an asterisk in the select * from albums query, it means to fetch all the data — * means “all the fields!”
Add the following SQL to your main.sql file (don’t erase the create statement from earlier!)
insert into albums values (1, '1989', 'Taylor Swift', '2014-10-27'); select * from albums;
 
notion image

Step Nine

Let’s add a few more records.
Delete the select * from albums; line from the previous step.
Add the following SQL to your main.sql file
insert into albums values (2, 'Remain in Light', 'The Talking Heads', '1980-10-08'); insert into albums values (3, 'Sour', 'Olivia Rodrigo', '2021-05-21'); insert into albums values (4, 'Hurry Up Tomorrow', 'The Weeknd', '2025-01-31'); insert into albums values (5, 'Guts', 'Olivia Rodrigo', '2023-09-08'); select * from albums;
notion image
💭
Think about it: What do you think would happen if we tried to insert a record with an ID that already exists, like another row with ID 1?
If you have time, try it! Did your prediction match what happened?

Step Ten

We can also select just the fields we want. Let’s select just the title and artist fields. Instead of using *, we’ll use the field names.
Add the following line to main.sql .
select title, artist from albums;
To keep things simple, let’s comment out the select * from albums; query, so that our output is clearer. Add two hyphens to the the beginning of the line to comment out a line. This makes the program ignore that line of SQL code.
Commenting is also used for your own notes! Try writing a comment to explain what you’re doing.
-- select * from albums; -- Print only the title and artist fields. select title, artist from albums;
 
notion image
notion image
 

🧮 COUNTing Records

One entry in the database is called a record. Let’s count how many records we have in our album table so far.

Step One

Comment out the previous select statement.
Enter the following query in main.sql.
select count(*) from albums;
 
notion image
notion image

Step Two - Using where

What if we want to ask, “How many albums by Olivia Rodrigo are in the table?”
Enter the following query in main.sql.
select count(*) from albums where artist = 'Olivia Rodrigo';
notion image
notion image
 

❓More Querying with where

Step One - Using where with Dates

What if we want to ask, “Which albums in the table came out before 2022?”
Enter the following query in main.sql.
select * from albums where release_date < '2022-01-01';
You can use the following operators to compare numbers and dates in SQL:
Operator
Meaning
=
equal to
<>
not equal to
>
greater than
<
less than
>=
greater than or equal to
<=
less than or equal to
notion image
notion image
 

👯‍♀️Using distinct

We can also use SQL to get a list of all the different artists in the albums table. Some artists, like Olivia, might appear twice, so we can use the distinct keyword to make sure she only shows up in the list once.
select distinct(artist) from albums;
 
notion image
notion image
 
📝
Practice | SQL Basics
Let’s practice what we’ve learned so far!
In main.sql, insert 3 additional albums into the database. Two of them should be from the same artist. You can estimate the release dates if you don’t want to look them up.
In main.sql, write a query to answer the question: “Which albums came out after the year 2015?” Select all fields in this query.
Write a query to answer the question: “Which artists released an album after the year 2015?”
  1. The only field that you need to select is the artist field.
  1. Use distinct to make sure that no artists show up more than once in the list.
📝
🌶️🌶️🌶️ Spicy Challenge | SQL Basics
These problems will require you to do some research on your own to find the right keywords to solve them! Try them out if you finish early.
  • Delete records:
    • Write a query that will deletes all records that have the artist “The Talking Heads.”
    • Hint: Try searching: ‘SQL delete statement’
  • Filter by Multiple Conditions:
    • Write a query to find albums released after 2015 by a specific artist (e.g., "Taylor Swift").
  • Count the Number of Albums Released After 2015:
    • Modify the query to return the number of albums released after 2015 instead of listing them.
  • Sort Albums by Release Year:
    • Modify the "albums released after 2015" query to order the results by release year (newest first).

Clean Up

Uncomment all the commented queries in main.sql. This file will be useful to revisit for examples of basic SQL queries as you continue to learn and practice on larger data sets!
 
page icon
For a summary of this lesson, check out the 3. SQL Basics One-Pager!