
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
Given a Database setup like this:
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
);
CREATE TABLE Posts (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
userId INT NOT NULL,
FOREIGN KEY (userId) REFERENCES Users(id)
);
INSERT INTO Users (name, email, age) VALUES
("Oscar","oscar@email.com", 25),
("Mario","mario@email.com", 30),
("Andrea","andrea@email.com", 40);
INSERT INTO Posts (title,content,userId) VALUES
("My First Post","My Name is Oscar.",1),
("Mario's New Post","Mario's blog is online!",2),
("Mario's Second Post","Hi mom!",2),
("Why Andrea is the Best","Some Post content Here",3);
Here are the solution and expected answers:
SELECT * FROM Posts WHERE userId IN
(SELECT id FROM Users WHERE age>=30);
SELECT * FROM Posts WHERE userId IN
(SELECT id FROM Users WHERE name LIKE "%ar%");
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.