π Selecting Data
Welcome to the Selecting Data module! This foundational learning path is designed to help you master the basics of querying data, particularly focusing on how to retrieve specific information from databases effectively.
π Creating SQL Tableβ
In this tutorial, you'll learn how to interpret and use rows in a database table. Tables are essential to storing structured data, and each row in a table represents a unique item or record.
Each row of a table represents a new item.
Each column of a table represents a specific attribute of the data, such as
id,name, orusername. These columns define the type of information stored for each item in the table.
For example, consider a table named Friends. Below is how a simple table might look:
- SQL Table
- SQL Code
- Output
| id | name | username |
|----|-----------------|------------------|
| 1 | John Doe | @johndoe |
| 2 | Jane Smith | @janesmith |
| 3 | Bob Johnson | @bobjohnson |
-- creating database
CREATE DATABASE my_database;
-- use the database you created
USE my_database;
-- Create the table
CREATE TABLE friends (
id INT PRIMARY KEY,
name VARCHAR(100),
username VARCHAR(100)
);
-- Insert data into the table
INSERT INTO friends (id, name, username) VALUES
(1, 'John Doe', '@johndoe'),
(2, 'Jane Smith', '@janesmith'),
(3, 'Bob Johnson', '@bobjohnson');
| id | name | username |
|---|---|---|
| 1 | John Doe | @johndoe |
| 2 | Jane Smith | @janesmith |
| 3 | Bob Johnson | @bobjohnson |
π§Ύ Explanationβ
The first row of this table contains:
id: 1name: John Doeusername: @johndoe
Each of the remaining rows represents a different friend, with unique ID, name, and username values.
β This is your first step in understanding how data is structured in relational databases. Once you master rows, you're on your way to writing powerful SQL queries!
π Querying a Table with FROMβ
To query data from a table, use the FROM clause followed by the table's name.
For example, consider a table named Friends. Below is how a simple table might look:
- SQL Table
- SQL Code
- Output
| id | name | email |
|----|-------------------|--------------------------|
| 1 | Tony Stark | ironman@avengers.com |
| 2 | Bruce Wayne | batman@gotham.com |
| 3 | Leia Organa | princess@rebellion.org |
SELECT name
FROM friends;
| id | name | |
|---|---|---|
| 1 | Tony Stark | ironman@avengers.com |
| 2 | Bruce Wayne | batman@gotham.com |
| 3 | Leia Organa | princess@rebellion.org |
When requesting data with SQL staments like SELECT, we say that we are making a query. From helps in select the name col from While not necessary but its a good practice to finish the sql queries with;
By following these best practices,
π Selecting Multiple Columns in SQLβ
- SQL Table
- SQL Code
- Output
| id | name | email |
|----|-------------------|--------------------------|
| 1 | Tony Stark | ironman@avengers.com |
| 2 | Bruce Wayne | batman@gotham.com |
| 3 | Leia Organa | princess@rebellion.org |
SELECT name, email
FROM friends;
| name | |
|---|---|
| Tony Stark | ironman@avengers.com |
| Bruce Wayne | batman@gotham.com |
| Leia Organa | princess@rebellion.org |
π§Ή Selecting Unique Values with DISTINCTβ
Sometimes your table might contain duplicate values, and you only want to see each unique value once in your result.
Thatβs where the DISTINCT keyword comes in!
- SQL Table
- SQL Code
- Output
| name | email | country |
|------|-------------------|----------|
| Sam | sam17@mail.com | England |
| Remy | rem@mail.com | France |
| Luis | luis_99@mail.com | France |
SELECT DISTINCT country
FROM subscribers;
| country |
|---|
| England |
| France |
β What You have Learnedβ
This module covers four essential topics in data selection:
-
Rows and Columns
Learn how to access specific rows and columns in a dataset or table, the building blocks of any query. -
Select Data
Understand the basicSELECTstatement to retrieve data from a database. -
Select Multiple Columns
Retrieve more than one column at a time in your queries to get the information you need all at once. -
Select Distinct Values
UseDISTINCTto eliminate duplicate records and identify unique entries within your dataset.