SQL Basics

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?

  1. data like users' music videos.

  2. 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?

  1. A programming language we use to select, modify, and insert data in databases.

  2. 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.
userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_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?

userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_99@mail.com
  1. Three user items, because it has three rows.

  2. 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?

userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_99@mail.com
  1. Each column stands for a different user item

  2. Properties of each user item, like a name or email

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.

userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_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

userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_99@mail.com

Practice


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
userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_99@mail.com

PRACTICE


When requesting data with SQL statements like SELECT, we say that we're making a query.

Users

userID

name

email

1

Sam

sam17@mail.com

2

Remy

rem@mail.com

3

Luis

luis_99@mail.com

PRACTICE


Code a query that selects all titles from the movies table with SELECT title followed by FROM movies;.

movies
titleyearrating
The Godfather19729.2
The Matrix19998.7
Alien19798.4

PRACTICE


To tell apart SQL keywords from column and table names, we write them in upper case. Tap on the code and type FROM.

Users
userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_99@mail.com

PRACTICE


While not necessary, it's considered good practice to finish your SQL queries with a semicolon ;.

Users
userIDnameemail
1Samsam17@mail.com
2Remyrem@mail.com
3Luisluis_99@mail.com

PRACTICE


.