Table of contents
INTRODUCTION
ROWS AND COLUMNS
Many apps you interact with store data like a username, password, or friends list.
This kind of data lives in a Database. Most databases are collections of tables storing different pieces of data.
What kind of data do you think a website like Hashnode stores?
data like users' music videos.
data like users' names, email, active streak, process.
data like users' names, email, active streak, process.
- SQL is a language for modifying, inserting, and accessing data from tables in a database.
Q) What is SQL?
A programming language we use to select, modify, and insert data in databases.
A programming language we use to code website interfaces
A programming language we use to select, modify, and insert data in databases.
- Tables are like spreadsheets storing information about a specific kind of data, like this table storing users' IDs, names, and emails.
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
Each row of a table represents a new item, like how the first row contains data about a user item's ID, name, and email address.
Q) If every row stores an item, how many user items does this table store?
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
Three user items, because it has three rows.
We can't know how many user items it stores.
Three user items, because it has three rows.
Properties of each user item, like a name
or email
Q) If rows stand for items, what do you think columns are?
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
Each column stands for a different user item
Properties of each user item, like a
name
oremail
Properties of each user item, like a name or email
Columns store properties of items, like how the email
column stores the email of each user item.
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
Select Data
If SQL is a language for manipulating data, which language instruction might we use to select data?
1 )DELETE
2 )SELECT
Use the SELECT
statement, followed by name
to select the values on the name
column.
Users
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
What do you think the FROM users
statement does?
SELECT name
FROM users;
1) It says which table we want to select the name
column from
2) It renames a column
It says which table we want to select the name column from
Each table has a name. Code FROM
followed by the table name users
to select data from the users
table.
Users | ||
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
When requesting data with SQL statements like SELECT
, we say that we're making a query.
Users | |||
userID | name | ||
1 | Sam | ||
2 | Remy | ||
3 | Luis |
Code a query that selects all titles from the movies
table with SELECT title
followed by FROM movies;
.
movies | ||
title | year | rating |
The Godfather | 1972 | 9.2 |
The Matrix | 1999 | 8.7 |
Alien | 1979 | 8.4 |
To tell apart SQL keywords from column and table names, we write them in upper case. Tap on the code and type FROM
.
Users | ||
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
While not necessary, it's considered good practice to finish your SQL queries with a semicolon ;
.
Users | ||
userID | name | |
1 | Sam | sam17@mail.com |
2 | Remy | rem@mail.com |
3 | Luis | luis_99@mail.com |
.