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”
 

💚 SQL Studio

We’ll use SQL Studio to write SQL queries throughout these lessons. Create an account and code along!
💡
SQL Studio is a web-based IDE made for Kode With Klossy scholars (created by KWK Alum Jasmine Wongphatarakul)! This platform was engineered specifically for our community to help master SQL through a professional, collaborative workflow.
SQL Studio runs on SQLite, which is one of many relational database management systems (RDBMS). Other popular RDBMS include PostgreSQL, MySQL, and SQL Server. They all use SQL, but each one has small differences in features and syntax. In this camp, we'll be using SQLite, but the core SQL concepts you learn here will apply across all of them!

🔢 Creating and Populating a Table

Step One

After you create a free SQL Studio account, click the “New Project” button in your dashboard.
 
notion image

Step Two

Give your new project a title, like “SQL Basics.”
You'll notice that the file already has a line in it: -- Write your SQL query here. This is a comment. Any line that starts with two hyphens (--) is a comment, and SQL will ignore it when you run your file. Comments are useful for leaving yourself reminders, explaining what your code does, or temporarily telling SQL to skip a line of code.
Pro tip: You can quickly comment or uncomment a line using the keyboard shortcut Cmd + / (Mac) or Ctrl + / (Windows). Try it out! Click on the comment line and press the shortcut to uncomment it, then press it again to comment it back out.
We'll use comments throughout this lesson as we build up our file, so keep this in mind!
notion image

Step Three

The IDE is pre-populated with a main.sql file where you can write your queries. Try writing the following statement in your main.sql file:
select 'Hello world!';
Take a moment to predict what you think will happen when you run this code. Then, hit Run!
When you hit run, you’ll see the output in the Console below your editor! 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 Console!
notion image

Step Four

Now let’s try updating the command in the main.sql file.
Note: The capitalization of SQL keywords like select doesn't matter. Writing SELECT, select, or even SeLeCt will all work the same way. You may see SQL written with uppercase keywords in other tutorials or in the workplace, and that's a common style convention. In this camp, all of our examples will use lowercase, but feel free to use whichever style you prefer!
select 'Hello from main.sql!';
Hit the Run button. What happens?
Do you see the unsaved indicator? To save your work in a file, you can either select the Save button next to the Run button or use the Cmd + S or Ctrl + S shortcut.
notion image
 

Step Five

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.
Data Type
What It Stores
Examples
integer
Whole numbers
0, 1, 2, 100
text
Any combination of letters, numbers, punctuation marks, and emoji
'Taylor Swift', '1989', 'Hello!'
date
Calendar values in YYYY-MM-DD format
'2014-10-27', '2023-09-08'
Type the SQL statement to create the table in your main.sql 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!
notion image

Step Six

Now, hit Run again without making any changes to your file. What happens?
You should see an error message: "Error: table albums already exists."
Every time you click Run, SQL Studio executes your entire file from top to bottom. The first time you ran the file, SQL created the albums table. The second time, it tried to create the same table again, but it was already there! SQL is letting you know it can't create something that already exists.
You will see this error any time you re-run a file that contains a create table statement for a table that has already been created. To avoid this, we recommend adding this line at the top of your file before your create table statement: drop table if exists albums;.
drop table if exists albums; create table albums ( id integer primary key, title text, artist text, release_date date );
This tells SQL to delete the table if it exists, so it can be created again from scratch!
Try running your file a few times to confirm that the error is gone! Remember, there won’t be any output from this step, but the error message should disappear!
notion image

Step Seven

Now, let’s add some data. We’ll use an insert statement to insert our first record into our albums table. 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');
Let's break down what's happening here:
  • insert into albums tells SQL that we want to add a new record to the albums table.
  • values (...) provides the data for that record. The values are listed in the same order as the fields we defined when we created the table: id, title, artist, and release_date.
notion image
 
Hit Run. Just like the create table statement, there won't be any output unless there is an error. If there is, pause and fix it before moving on.
Notice that text values like '1989' and 'Taylor Swift' are wrapped in single quotes, while the integer 1 is not. In SQL, text values always need to be wrapped in single quotes so that SQL knows to treat them as text. Even though 1989 looks like a number, it's the title of the album, so we treat it as text! Dates like '2014-10-27' are also wrapped in single quotes. Integers and other numbers do not need quotes.

Step Eight

We've inserted data into our table, but how do we know it's actually there? We can use a select query to retrieve data from a table and output it so we can see it.
Add the following SQL to your main.sql file:
select * from albums;
When we use an asterisk (*) in the query, it means “all the fields.” So select * from albums tells SQL to fetch every field for every record in the albums table!
Hit Run. You should see your record displayed in the Console!
Curious what “Export CSV” does in the console? It downloads the results of your most recent query as a CSV file directly to your computer!
notion image

Step Nine

Let’s add a few more records.
Comment out the select * from albums; line from the previous step. Since we're adding new queries, let's comment out the previous select statement so our Console output only shows the results of the newest query.
Add the following SQL to your main.sql file.
insert into albums values (2, 'Remain in Light', '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. 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;
count() is a built-in SQL function that counts the number of records returned by a query. By passing in *, we're telling SQL to count all records in the albums table.
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
 

❓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
 

👯‍♀️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
📝
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.
🤖 AI Connection
Stuck on what albums to add? Ask an AI tool: "Give me the title, artist, and release date for 3 albums released in the last 5 years. Two should be by the same artist. Format each as a SQL insert into statement for a table called albums with fields: id, title, artist, release_date." Before running the AI-generated SQL, read each statement carefully. Did the AI use the correct data types? Are text values wrapped in single quotes? Are the dates in YYYY-MM-DD format? Fix any mistakes before you run it.
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.
🌶️🌶️🌶️ Click here for a Spicy Challenge!
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 “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 out 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!
 
 
Â