SQL Classic Exercise: Computer Store

Table Structure

The table structure used in this article is as follows:

Screenshot 2023-03-02 pm 5.27.32.png

The following is the statement to create the table:

-- 厂商表
CREATE TABLE Manufacturers (
  Code INTEGER NOT NULL PRIMARY KEY, -- 编号,主键
  Name VARCHAR(255) NOT NULL, -- 名称
);

-- 产品表
CREATE TABLE Products (
  Code INTEGER NOT NULL PRIMARY KEY, -- 编号,主键
  Name VARCHAR(255) NOT NULL, -- 名称
  Price DECIMAL NOT NULL, -- 价格
  Manufacturer INTEGER NOT NULL, -- 厂商编号
  FOREIGN KEY (Manufacturer) REFERENCES Manufacturers(Code) -- 外键,引用厂商表
);

-- 测试数据
INSERT INTO Manufacturers(Code,Name) 
VALUES (1,'Sony'),
       (2,'Creative Labs'),
       (3,'Hewlett-Packard'),
       (4,'Iomega'),
       (5,'Fujitsu'),
       (6,'Winchester');

INSERT INTO Products(Code,Name,Price,Manufacturer) 
VALUES (1,'Hard drive',240,5),
       (2,'Memory',120,6),
       (3,'ZIP drive',150,4),
       (4,'Floppy disk',5,6),
       (5,'Monitor',240,1),
       (6,'DVD drive',180,2),
       (7,'CD drive',90,2),
       (8,'Printer',270,3),
       (9,'Toner cartridge',66,3),
       (10,'DVD burner',180,2);

practice questions

View all product names

SELECT Name FROM Products;

View all product names and prices

SELECT Name, Price FROM Products;

View product names whose price is less than or equal to 200

SELECT Name FROM Products WHERE Price <= 200;

Check out products priced between 60 and 120

/* 使用 AND 运算符*/
SELECT * FROM Products
WHERE Price >= 60 AND Price <= 120;

/* 使用 BETWEEN 运算符*/
SELECT * FROM Products
WHERE Price BETWEEN 60 AND 120;

View the names and prices of all products, and the prices are displayed in cents

SELECT Name, Price * 100 AS PriceCents FROM Products;

Calculate the average price of all products

SELECT AVG(Price) FROM Products;

Calculate the average price of all products with vendor number 2

SELECT AVG(Price) FROM Products WHERE Manufacturer=2;

Count the number of products whose price is greater than or equal to 180

SELECT COUNT(*) FROM Products WHERE Price >= 180;

View product names and prices whose prices are greater than or equal to 180, sort by price in descending order and name in ascending order

SELECT Name, Price 
FROM Products
WHERE Price >= 180
ORDER BY Price DESC, Name;

View all products and their manufacturer information

SELECT * FROM Products, Manufacturers
WHERE Products.Manufacturer = Manufacturers.Code;

View all product names, prices and manufacturer names

SELECT Products.Name, Price, Manufacturers.Name
FROM Products 
JOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code;

Count the average product price of each manufacturer and display the manufacturer number

SELECT AVG(Price), Manufacturer
FROM Products
GROUP BY Manufacturer;

Count the average product price of each manufacturer and display the name of the manufacturer

SELECT AVG(Price), Manufacturers.Name
FROM Products 
JOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code
GROUP BY Manufacturers.Name;

View the names of manufacturers whose average product price is greater than or equal to 150

SELECT AVG(Price), Manufacturers.Name
FROM Products 
JOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code
GROUP BY Manufacturers.Name
HAVING AVG(Price) >= 150;

Check out the cheapest product names and prices

SELECT Name, Price
FROM Products
ORDER BY price ASC
LIMIT 1;

/* 如果存在多个最便宜的产品,嵌套子查询可以返回多条记录 */
SELECT Name, Price
FROM Products
WHERE Price = (SELECT MIN(Price) FROM Products);

View each vendor and its most expensive product name and price

SELECT A.Name, A.Price, F.Name
FROM Products A 
JOIN Manufacturers F
ON A.Manufacturer = F.Code
AND A.Price =
     (
       SELECT MAX(A.Price)
       FROM Products A
       WHERE A.Manufacturer = F.Code
     );

View the names of manufacturers whose average product price is greater than 145 and produce at least 2 products

SELECT m.Name, Avg(p.price) AS p_price, COUNT(p.Manufacturer) AS m_count
FROM Manufacturers m, Products p
WHERE p.Manufacturer = m.code
GROUP BY m.Name
HAVING Avg(p.price) >= 150 AND COUNT(p.Manufacturer) >= 2;

add a new product

The information of the new product is as follows: the name is Loudspeakers, the price is 70, and the manufacturer number is 2.

INSERT INTO Products( Code, Name , Price , Manufacturer)
  VALUES ( 11, 'Loudspeakers' , 70 , 2 );

Renamed product number 8 to "Laser Printer"

UPDATE Products
SET Name = 'Laser Printer'
WHERE Code = 8;

10% off all products

UPDATE Products
SET Price = Price - (Price * 0.1);

10% discount on products with a price of 120 or more

UPDATE Products
SET Price = Price - (Price * 0.1)
WHERE Price >= 120;

 

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/131955611