0% found this document useful (0 votes)
21 views1 page

SQL Task

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

SQL Task

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

-- Create table for users

CREATE TABLE users (


user_id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE
);
-- Create table for books
CREATE TABLE books (
book_id INT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
author VARCHAR(100) NOT NULL
);
-- Create table for checkouts
CREATE TABLE checkouts (
checkout INT PRIMARY KEY,
user_id INT,
book_id INT,
checkout date DATE NOT NULL,
return_date DATE,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (book_id) REFERENCES books(book_id)
);
-- Insert records into users
INSERT INTO users (user_id, username, email) VALUES
(1, 'John', '[email protected]'),
(2, 'Jane', '[email protected]'),
(3, 'Doe', '[email protected]');
-- Insert records into books
INSERT INTO books (book_id, title, author) VALUES
(1, 'SQL for Beginners', 'John Mayor'),
(2, 'Advanced SQL', 'Jane Doe'),
(3, 'Database Design', 'John Mayor');
-- Insert records into checkouts
INSERT INTO checkouts (checkout, user_id, book_id, checkout date, return date)
VALUES
(1, 1, 1, '2024-01-01', '2024-01-10'),
(2, 2, 2, '2024-02-01', NULL),
(3, 1, 3, '2024-03-01', NULL);
SELECT * FROM users;
SELECT * FROM users;
2. List title and author of books:
SELECT title, author FROM books;
3. List checkout date and return date for a user named 'hani' and author 'hani
prajapati':
SELECT c.checkout_date, c.return_date
FROM checkouts c
JOIN users u ON c.user_id = u.user_id
JOIN books b ON c.book_id = b.book_id
WHERE username = 'John' AND b.author = 'hani prajapati';
4. List all users with the user name 'hani':
SELECT * FROM users WHERE user_name = 'hani';
5. List all users whose return date is NULL:
SELECT u.*
FROM users u
JOIN checkouts c ON u.user_id = c.user_id
WHERE c.return_date IS NULL;

You might also like