ECNU数据库作业——Lab4

Lab 4

EX1

How many countries does each continent have? List the continent id, continent name and the number of countries?

SELECT COUNT(countries.CountryName) AS Num_Country, continents.Continent
FROM countries
JOIN continents ON continents.ContId = countries.Continent
GROUP BY countries.Continent
image-20231114154748333

EX2

What is the average horsepower of the cars before 1980?

-- 注意horsepower属性数据类型为text,且其中有'null'值
SELECT AVG(CAST(cars_data.Horsepower AS INTEGER)) AS avg_horsepower
FROM cars_data
WHERE cars_data.Year < 1980 AND cars_data.Horsepower != 'null'
image-20231114154651602

EX3

How many models does each car maker produce? List maker full name, id and the number.

SELECT car_makers.FullName, car_makers.Id, COUNT(model_list.Model) AS Num_Model
FROM model_list
JOIN car_makers ON car_makers.Id = model_list.Maker
GROUP BY model_list.Maker

image-20231114150816689image-20231114150853773

EX4

What is the average edispl of the cars of model volvo?

SELECT AVG(cars_data.Edispl) AS avg_edispl
FROM cars_data
JOIN car_names ON car_names.MakeId = cars_data.Id
WHERE car_names.Model = 'volvo'
image-20231114142814877

EX5

What is the model of the car with the smallest amount of horsepower?

SELECT DISTINCT car_names.Model, cars_data.Horsepower
FROM car_names
JOIN cars_data ON cars_data.Id = car_names.MakeId
WHERE CAST(cars_data.Horsepower AS INTEGER) = (
	SELECT MIN(CAST(cars_data.Horsepower AS INTEGER))
	FROM cars_data
	WHERE cars_data.Horsepower != 'null'
) 
image-20231114151859588

EX6

Find the model of the car whose weight is below the average weight.

SELECT DISTINCT car_names.Model
FROM car_names
JOIN cars_data ON cars_data.Id = car_names.MakeId
WHERE cars_data.Weight < (
	SELECT AVG(cars_data.Weight)
	FROM cars_data
)

image-20231114150635363image-20231114150658292

EX7

What is the maximum accelerate for different number of cylinders?

SELECT MAX(cars_data.Accelerate) AS Max_Accelerate, cars_data.Cylinders
FROM cars_data
GROUP BY cars_data.Cylinders
image-20231114133724662

EX8

How many car makers are there in each continents? List the continent name and the count?

SELECT continents.Continent AS continent, COUNT(car_makers.Country) AS Num_Makers
FROM continents
JOIN countries ON countries.Continent = continents.ContId
JOIN car_makers ON car_makers.Country = countries.CountryId
GROUP BY continents.Continent
image-20231114134253632

EX9

Which of the countries has the most car makers? List the country name.

SELECT countries.CountryName
FROM countries
JOIN car_makers ON car_makers.Country = countries.CountryId
GROUP BY countries.CountryId
HAVING COUNT(car_makers.Maker) = (
    SELECT MAX(count_makers)
    FROM (
        SELECT COUNT(car_makers.Maker) AS count_makers
        FROM car_makers
        JOIN countries ON car_makers.Country = countries.CountryId
        GROUP BY countries.CountryId
    )
)
image-20231114135452393

EX10

What are the countries having at least one car maker? List name and id.

SELECT DISTINCT countries.CountryName AS name, countries.CountryId AS ID
FROM countries
LEFT JOIN car_makers ON car_makers.Country = countries.CountryId
WHERE car_makers.Maker IS NOT NULL
image-20231114140130215

EX11

Which countries in europe have at least 3 car manufacturers?

SELECT countries.CountryName
FROM countries
JOIN car_makers ON countries.CountryId = car_makers.Country
JOIN continents ON continents.ContId = countries.Continent
GROUP BY car_makers.Country
HAVING COUNT(car_makers.Maker) >= 3 AND continents.Continent = 'europe'
image-20231114141646871

EX12

What is the maximum horsepower and the make of the car models with 3 cylinders?

SELECT MAX(CAST(cars_data.Horsepower AS INTEGER)) AS Max_Horsepower, car_names.Make
FROM cars_data
JOIN car_names ON car_names.MakeId = cars_data.Id
WHERE cars_data.Cylinders = 3 AND cars_data.Horsepower != 'null'
image-20231114142606757
  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值