Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Biydaalt

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

Biydaalt

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

CREATE database biydaalt

use biydaalt

CREATE TABLE Customers


(
CustomerID INT PRIMARY KEY IDENTITY(1,1),
FirstName VARCHAR(50),
LastName VARCHAR(50),
City VARCHAR(50)
);
CREATE TABLE Orders
(
OrderID INT PRIMARY KEY IDENTITY(1,1),
CustomerID INT,
OrderDate date,
TotalAmount INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

CREATE TABLE Categories


(
CategoryID INT PRIMARY KEY IDENTITY(1,1),
CategoryName VARCHAR(100)
);
CREATE TABLE Products
(
ProductID INT PRIMARY KEY IDENTITY(1,1),
ProductName VARCHAR(100),
Category INT,
Price INT,
FOREIGN KEY (Category) REFERENCES Categories(CategoryID)
);
CREATE TABLE OrderDetails
(
OrderDetailID INT PRIMARY KEY IDENTITY(1,1),
OrderID INT,
ProductID INT,
Quantity INT,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
INSERT INTO Categories
(CategoryName)
VALUES
('Electronics'),
('Books'),
('Clothing'),
('Toys');

INSERT INTO Customers


(FirstName, LastName, City)
VALUES
('John', 'Doe', 'New York'),
('Jane', 'Smith', 'Los Angeles'),
('Sam', 'Wilson', 'Chicago'),
('Emily', 'Jones', 'Houston');

INSERT INTO Products


(ProductName, Category, Price)
VALUES
('Laptop', 1, 1000),
('Smartphone', 1, 700),
('Novel', 2, 20),
('T-Shirt', 3, 15),
('Action Figure', 4, 25);

INSERT INTO Orders


(CustomerID, OrderDate, TotalAmount)
VALUES
(1, '2024-10-31', 1500),
(2, '2024-10-30', 700),
(3, '2024-10-29', 50),
(4, '2024-10-28', 25);

INSERT INTO OrderDetails


(OrderID, ProductID, Quantity)
VALUES
(1, 1, 1),
(1, 2, 1),
(2, 2, 1),
(3, 3, 2),
(4, 5, 1);
Асуулга:
1. Customers ийн мэдээллийг үзүүлэх
SELECT FirstName, LastName, City FROM Customers;

πFirstName,LastName,City(Customers)

2. Customer болон order-ийн мэдээллийг үзүүлэх


SELECT o.OrderID, c.FirstName, c.LastName, o.OrderDate
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID;

πOrderID,FirstName,LastName,OrderDate(Orders⋈Customers)

3. Products-н Category-г үзэх


SELECT p.ProductName, c.CategoryName, p.Price
FROM Products p
LEFT JOIN Categories c ON p.Category = c.CategoryID;

πProductName,CategoryName,Price(Products⋈Categories)

4. Order доторх quantity-г олох


SELECT od.OrderID, SUM(od.Quantity) AS TotalQuantity
FROM OrderDetails od
GROUP BY od.OrderID;

πOrderID,SUM(Quantity) AS TotalQuantity(OrderDetails) GROUP BY OrderID

5. Сүүлд хийсэн order болон хэрэглэгчийн мэдээлэл


SELECT TOP 1 c.FirstName, c.LastName, o.OrderDate
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
ORDER BY o.OrderDate DESC;

πFirstName,LastName,OrderDate(σOrderDate=max(OrderDate)(Orders)⋈Customers)

You might also like