0% found this document useful (0 votes)
10 views4 pages

New Text Document

The document outlines SQL commands for creating and managing a database with tables for Books, Members, Products, Customers, and Orders. It includes commands for inserting, updating, deleting, and querying data across these tables, demonstrating various SQL operations such as joins and aggregations. The structure supports a library and e-commerce system, allowing tracking of book borrowing and product sales.

Uploaded by

ibrahimmohsen305
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)
10 views4 pages

New Text Document

The document outlines SQL commands for creating and managing a database with tables for Books, Members, Products, Customers, and Orders. It includes commands for inserting, updating, deleting, and querying data across these tables, demonstrating various SQL operations such as joins and aggregations. The structure supports a library and e-commerce system, allowing tracking of book borrowing and product sales.

Uploaded by

ibrahimmohsen305
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/ 4

W2 Q1

create table Book(


Book_id int primary key auto_increment,
title varchar(50),
auther varchar(50),
ISBN varchar(50),
pub_year date
);

create table member1(


Member_id int primary key auto_increment,
name_ varchar(50),
email varchar(50) unique,
phone char(11)
);

create table borrow(


borrow_id int primary key auto_increment,
borrow_date date,
return_date date ,
member_id int,
book_id int,
FOREIGN KEY (book_id) REFERENCES Book(Book_id),
FOREIGN KEY (member_id) REFERENCES member1(Member_id)

);

W3 Q1
CREATE TABLE Products (
ProductID INT PRIMARY KEY AUTO_INCREMENT,
ProductName VARCHAR(100),
Price DECIMAL(10, 2),
Stock INT
);

INSERT INTO Products (ProductName, Price, Stock) VALUES ('Laptop', 999.99, 50);

SELECT * FROM Products;

UPDATE Products SET Price = 1099.99 WHERE ProductID = 1;

DELETE FROM Products WHERE ProductID = 1;

w4

CREATE TABLE Products (


ProductID INT PRIMARY KEY AUTO_INCREMENT,
ProductName VARCHAR(50),
Category VARCHAR(50),
Price DECIMAL(10, 2),
StockQuantity INT
);

INSERT INTO Products (ProductName, Category, Price, StockQuantity)


VALUES
('Laptop', 'Electronics', 999.99, 10),
('Phone', 'Electronics', 499.99, 25),
('Desk', 'Furniture', 150.00, 50),
('Lamp', 'Furniture', 75.50, 100),
('Tablet', 'Electronics', 299.99, 15);

SELECT *
FROM Products
ORDER BY Price DESC;

SELECT DISTINCT Category


FROM Products;

SELECT *
FROM Products
WHERE ProductName LIKE 'L%';

SELECT *
FROM Products
WHERE Price BETWEEN 100 AND 500;

SELECT *
FROM Products
ORDER BY Price DESC
LIMIT 3;

SELECT COUNT(*) AS TotalProducts


FROM Products;

SELECT Category, SUM(StockQuantity) AS TotalStock


FROM Products
GROUP BY Category;

SELECT Category, SUM(StockQuantity) AS TotalStock


FROM Products
GROUP BY Category
HAVING SUM(StockQuantity) > 100;

w5
CREATE TABLE Products (
ProductID INT PRIMARY KEY AUTO_INCREMENT,
ProductName VARCHAR(50),
Price DECIMAL(10, 2)
);

INSERT INTO Products (ProductName, Price)


VALUES
('Laptop', 999.99),
('Phone', 499.99),
('Tablet', 299.99);

CREATE TABLE Customers (


CustomerID INT PRIMARY KEY AUTO_INCREMENT,
CustomerName VARCHAR(50),
Email VARCHAR(50)
);

INSERT INTO Customers (CustomerName, Email)


VALUES
('John Doe', '[email protected]'),
('Jane Smith', '[email protected]'),
('Bob Johnson', '[email protected]');

CREATE TABLE Orders (


OrderID INT PRIMARY KEY AUTO_INCREMENT,
ProductID INT,
CustomerID INT,
OrderDate DATE,
Quantity INT,
FOREIGN KEY (ProductID) REFERENCES Products(ProductID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

INSERT INTO Orders (ProductID, CustomerID, OrderDate, Quantity)


VALUES
(1, 1, '2025-03-01', 1),
(2, 2, '2025-03-02', 2),
(3, 1, '2025-03-03', 1);

SELECT
c.CustomerName,
p.ProductName,
o.OrderDate,
o.Quantity
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID
INNER JOIN Products p ON p.ProductID = o.ProductID;

SELECT
c.CustomerName,
p.ProductName,
o.OrderDate,
o.Quantity
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
LEFT JOIN Products p ON p.ProductID = o.ProductID;
SELECT
c.CustomerName,
p.ProductName,
o.OrderDate,
o.Quantity
FROM Customers c
RIGHT JOIN Orders o ON c.CustomerID = o.CustomerID
RIGHT JOIN Products p ON p.ProductID = o.ProductID;

SELECT
c.CustomerName,
p.ProductName,
o.OrderDate,
o.Quantity
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
LEFT JOIN Products p ON p.ProductID = o.ProductID
UNION
SELECT
c.CustomerName,
p.ProductName,
o.OrderDate,
o.Quantity
FROM Customers c
RIGHT JOIN Orders o ON c.CustomerID = o.CustomerID
RIGHT JOIN Products p ON p.ProductID = o.ProductID
WHERE c.CustomerID IS NULL;

You might also like