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.
- 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
selectkeyword means that we are trying to pick out some information from the table weâre querying. Weâre selecting that information. favorite_coloris the information we want to select, andkwk_scholarsis the table that it lives in.fromis another one of those standard keywords.distinctis 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_scholarstable 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
agecolumn stores numbers, while thenamecolumn 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.
Â
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!
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!
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.Â
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:- A name:
albums
- 4 fields:
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.title, a text field, which will store the title of the album.artist, a text field, which will store the name of the artist.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!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!
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 albumstells SQL that we want to add a new record to thealbumstable.
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, andrelease_date.
Â
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!
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;
Â
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;
Â
đ§ŽÂ 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.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';
Â
â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 |
Â
đŻââď¸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;
Â
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?â
- The only field that you need to select is the
artistfield.
- Use
distinctto 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!For a summary of this lesson, check out the 3. SQL Basics One-Pager!
Â
Â
Â