
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
We don't always want every single record from our table. Very often we want to grab data about a specific item in our table. For instance, if we have a user's email or ID, and we want to grab the date they joined our application by looking up their 'createdAt' date we would need to 'filter' our select query down.
We can do this by attached a "WHERE" statement to the end of our "SELECT" statement.
If we wanted to grab just the user who's ID is "2", we would add to the end of our statement like so:
SELECT * FROM Users WHERE id=2;
We can combine that with our previous column selection to grab just the date:
SELECT createdAt FROM Users WHERE id=2;
We can use a 'WHERE' statement for any column. We can use the id, the name, the email or the dates.
Where statements can filter in more complex ways than just checking to see if some value is EQUAL to another.
Let's say we had a Users table with ages.
CREATE TABLE Users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
age INT NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
INSERT INTO Users (name, email, age) VALUES
("Oscar","oscar@email.com", 25),
("Mario","mario@email.com", 30),
("Andrea","andrea@email.com", 40);
I've gone ahead and added 3 users to this table with ages "25", "30", and "40". If we wanted to- we could grab all the user's based on their age:
SELECT * FROM Users WHERE age > 25;
See if you can come up with 3 different ways to grab each user. Grab a unique user by their age, name, and email.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.