-- LINK: The_computer_store-- 1.1 Select the names of all the products in the store.SELECT name FROM products LIMIT10;Select*from products LIMIT10;-- 1.2 Select the names and the prices of all the products in the store.SELECT name , price FROM products LIMIT10;-- 1.3 Select the name of the products with a price less than or equal to $200.SELECT name FROM products
WHERE price <=200;-- 1.4 Select all the products with a price between $60 and $120.SELECT*FROM products
WHERE price <120and price >60;select*from products
where price between60and120;-- 1.5 Select the name and price in cents (i.e., the price must be multiplied by100).SELECT name,(price)*100FROM products;---以美分显示,concat连接select name, concat(price*100,' cents')from products;-- 1.6 Compute the average price of all the products.SELECTavg(price)FROM products;-- 1.7 Compute the average price of all products with manufacturer code equal to 2.SELECTavg(price)FROM products
WHERE manufacturer =2;-- 1.8 Compute the number of products with a price larger than or equal to $180.SELECTcount(*)FROM products
WHERE price >=180;-- 1.9 Select the name and price of all products with a price larger than or equal to $180, and sort first by price (in descending order), and then by name (in ascending order).SELECT name, price FROM products
WHERE price >=180ORDERby price desc;SELECT name, price FROM products
WHERE price >=180ORDERby name;---同时排序SELECT name, price FROM products
WHERE price >=180ORDERby price desc, name asc;-- 1.10 Select all the data from the products, including all the data for each product's manufacturer.------两表连接---select a.*, b.name from products a join Manufacturers b on(a.manufacturer = b.code);select a.*, b.name from products a, Manufacturers b where a.manufacturer = b.code;-- 1.11 Select the product name, price, and manufacturer name of all the products.SELECT name, price, manufacturer FROM products;---select a.name, a.price, b.name
from products a join Manufacturers b on(a.manufacturer = b.code);SELECT Products.Name, Price, Manufacturers.Name
FROM Products INNERJOIN Manufacturers
ON Products.Manufacturer = Manufacturers.Code;-- 1.12 Select the average price of each manufacturer's products, showing only the manufacturer's code.SELECTavg(price), manufacturer FROM products
groupby manufacturer;-- 1.13 Select the average price of each manufacturer's products, showing the manufacturer's name.SELECTavg(price), name FROM products
groupby name;---selectavg(a.price), b.name
from Products a join Manufacturers b
on a.manufacturer = b.code
groupby b.name;-- 1.14 Select the names of manufacturer whose products have an average price larger than or equal to $150.???????????????SELECT name,avg(price)as ap FROM products
WHERE ap >=150groupby name;----selectavg(a.price), b.name
from Manufacturers b join Products a
on b.code = a.Manufacturer
groupby b.name
havingavg(a.price)>=150;SELECTAVG(Price), Manufacturers.Name
FROM Products, Manufacturers
WHERE Products.Manufacturer = Manufacturers.Code
GROUPBY Manufacturers.Name
HAVINGAVG(Price)>=150;-- 1.15 Select the name and price of the cheapest product.SELECT name FROM products
ORDERby price
LIMIT1;SELECT name FROM products
WHERE price =(SELECTmin(price)FROM products
);-- 1.16 Select the name of each manufacturer along with the name and price of its most expensive product.SELECT manufacturer, name, price FROM products
ORDERby price descLIMIT1;---select a.Name,max(a.price), b.Name
from Manufacturers b join Products a
on a.Manufacturer = b.code
groupby b.name;SELECT Products.Name,MAX(Price), Manufacturers.Name
FROM Products, Manufacturers
WHERE Manufacturer = Manufacturers.Code
GROUPBY Manufacturers.Name;select p.name,m.name,p.price from products p join manufacturers m on p.manufacturer = m.code where p.price in(selectmin(a.price)from Manufacturers b join Products a
on a.Manufacturer = b.code
groupby b.name);-- 1.17 Add a new product: Loudspeakers, $70, manufacturer 2.INSERTINTO Products(Code,Name,Price,Manufacturer)VALUES(11,'Loudspeakers',70,2);-- 1.18 Update the name of product 8 to "Laser Printer".UPDATE products
set name ='Laser Printer'where code =8;-- 1.19 Apply a 10% discount to all products.UPDATE products
set price =0.9* price;-- 1.20 Apply a 10% discount to all products with a price larger than or equal to $120.UPDATE products
set price =0.9* price
WHERE price >=120;---------判断被整除------SELECT*FROM products
WHERE price in(SELECT price from products where price %100=0);