This repository contains SQL queries for managing and analyzing data in a sample database dab15.
The project demonstrates basic to advanced SQL operations on three main tables:
- books
- customers
- orders
sql_project/
βββ queries.sql # All SQL queries
βββ README.md # Project documentation
create database dab15;
use dab15;select * from books
where Genre="Fiction";select * from books
where (Published_Year) > 1950;select * from customers
where Country='Canada';select * from orders
where Order_Date > '2023-11-01' and Order_Date < '2023-11-30';select sum(Stock) as total_stock
from books;select max(Price) from books;
select * from books
where Price=(select max(Price) from books);
select * from books
order by Price desc
limit 1;select * from orders
where Quantity > 1;select * from orders
where Total_Amount > 20;select distinct(Genre)
from books;select * from books
order by Stock
limit 1;select sum(Total_Amount) as revenue
from orders;select b.Genre, sum(o.Quantity) as Total_Books_Sold
from books as b
join orders as o on b.Book_ID = o.Book_ID
group by b.Genre;select avg(Price) as average_price
from books
where Genre="Fantasy";select c.Customer_ID, c.Name, count(o.Order_ID) as total_order
from customers as c
join orders as o on c.Customer_ID = o.Customer_ID
group by c.Customer_ID, c.Name
having total_order >= 2;select Book_ID, count(Order_ID) as order_count
from orders
group by Book_ID
order by order_count desc;select Book_ID, Title, Genre, Price
from books
where Genre="Fantasy"
order by Price desc
limit 3;select b.Author, sum(o.Quantity) as total_quantity
from books as b
join orders as o on b.Book_ID = o.Book_ID
group by b.Author;select c.City, o.Total_Amount
from orders as o
join customers as c on c.Customer_ID = o.Customer_ID
where o.Total_Amount > 30;select c.Customer_ID, c.Name, sum(o.Total_Amount) as total_spent
from customers as c
join orders as o on c.Customer_ID = o.Customer_ID
group by c.Customer_ID, c.Name
order by total_spent desc
limit 1;select b.Book_ID, b.Title, b.Stock, coalesce(sum(o.Quantity),0) as total_quantity,
(b.Stock - coalesce(sum(o.Quantity),0)) as remaining_quantity
from books as b
left join orders as o on b.Book_ID = o.Book_ID
group by b.Book_ID, b.Title, b.Stock;- Clone the repository
git clone https://github.com/your-username/sql_project.git
- Import the database schema into your SQL server.
- Run queries from queries.sql or directly from the README.
- Fork the repo
- Create a feature branch (
git checkout -b feature-name) - Commit your changes
- Submit a pull request
β This project is designed for SQL practice and can be extended for real-world database management.