Skip to content

Instantly share code, notes, and snippets.

@thevipulvats
Created January 28, 2025 16:13
Show Gist options
  • Save thevipulvats/1e74116206bc5e107613b6c28cfcd3d0 to your computer and use it in GitHub Desktop.
Save thevipulvats/1e74116206bc5e107613b6c28cfcd3d0 to your computer and use it in GitHub Desktop.
Filtering results (WHERE)
CREATE DATABASE bookstore;
USE bookstore;
CREATE TABLE books (
book_id INT PRIMARY KEY,
title VARCHAR(100),
author VARCHAR(50),
price DECIMAL(10,2),
publication_date DATE,
category VARCHAR(30),
in_stock INT
);
INSERT INTO books VALUES
(1, 'The MySQL Guide', 'John Smith', 29.99, '2023-01-15', 'Technology', 50),
(2, 'Data Science Basics', 'Sarah Johnson', 34.99, '2023-03-20', 'Technology', 30),
(3, 'Mystery at Midnight', 'Michael Brown', 19.99, '2023-02-10', 'Mystery', 100),
(4, 'Cooking Essentials', 'Lisa Anderson', 24.99, '2023-04-05', 'Cooking', 75);
INSERT INTO books VALUES
(5, 'Cook Book', null, 24.99, '2023-04-05', 'Cooking', 75);
INSERT INTO books VALUES
(6, 'Mini Cook Book', 'Gohn Smith', 24.99, '2023-04-05', 'Cooking', 75);
select * from books where category='Technology' ;
SELECT title, price FROM books WHERE price < 30.00;
SELECT title, publication_date FROM books
WHERE publication_date >= '2023-03-01';
-- Logical Operators
select * from books where category = 'Technology' and price < 30;
select * from books where category = 'Technology' or price < 30;
select * from books where (category = 'Technology' or category = 'Mystery') and price < 25;
select * from books where not category = 'Technology';
-- Finding NULL values
select * from books where author is null;
select * from books where author is not null;
-- Pattern matching
select * from books where title like '%SqL%';
select * from books where title like 'the%';
select * from books where title like binary '%SQL%';
select * from books where author like '_ohn%';
-- RANGE OPERATORS
select * from books where price between 20 and 30;
select * from books where category in (
'Technology', 'MysterY', 'Science');
SELECT * FROM books
WHERE price BETWEEN 20.00 AND 40.00
AND publication_date >= '2023-01-01';
-- SUBQUERIES
select * from books where price > ( select avg(price) from books );
select * from books where category in (
select category from books where in_stock > 20
);
-- Find all books published in 2023 that cost less than the average book price
SELECT title, price, publication_date
FROM books
WHERE YEAR(publication_date) = 2023
AND price < (SELECT AVG(price) FROM books);
-- List all technology books with "data" in the title that have more than 50 copies in stock
SELECT title, category, in_stock
FROM books
WHERE category = 'Technology'
AND title LIKE '%data%'
AND in_stock > 50;
-- Find books that are either in the Technology category with price > $30 or in the Mystery category with price < $20
SELECT title, category, price
FROM books
WHERE (category = 'Technology' AND price > 30.00)
OR (category = 'Mystery' AND price < 20.00);
-- List all books where the author's name contains either 'son' or 'th' and were published after March 2023
SELECT title, author, publication_date
FROM books
WHERE (author LIKE '%son%' OR author LIKE '%th%')
AND publication_date > '2023-03-31';
@manasrpatra146
Copy link

thankyou sir ❤️❤️❤️❤️❤️❤️🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

@PramodSolapur
Copy link

Thank you sir, doing great work.

@rohan-2702
Copy link

Thank Your Sir.

@AbhishekWadile
Copy link

Thank You sir

@developer-11
Copy link

Thank You Sir

@premanand98123
Copy link

Your teaching is best !!!!!!

@Dev22Patel
Copy link

nice questions for hands on practice after clearing the concept

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment