SQL 没有连接的子查询
SQL sub-queries with no joins
我在修改的时候真的很纠结其中一个问题。我想你们可以帮帮我。
这里我有两个表 book 和 branch
问题是:
List the title and author of books whose sales are greater than the
average sales. For each such book, also list the difference between
its sales and the average sales. The column of differences in the
table of results should be named "Difference".
这是我试过的
SELECT title, authorFROM book
WHERE sales > AVG (sales) ( SELECT bookNo AS Difference
FROM book
WHERE Difference= sales-AVG(sales));
您想要创建一个 table 包含三列的结果:title
、author
和 Difference
(其销售额与平均销售额之间的差异)。在 sql 中,您可以在 select 表达式中进行数学运算,因此您只需将 (sales-AVG(sales))
添加到列列表中即可。要指定名称,您可以使用关键字 AS
.
SELECT title, author, (sales-AVG(sales)) AS Difference FROM book WHERE sales>AVG(sales)
我在修改的时候真的很纠结其中一个问题。我想你们可以帮帮我。
这里我有两个表 book 和 branch
问题是:
List the title and author of books whose sales are greater than the average sales. For each such book, also list the difference between its sales and the average sales. The column of differences in the table of results should be named "Difference".
这是我试过的
SELECT title, authorFROM book
WHERE sales > AVG (sales) ( SELECT bookNo AS Difference
FROM book
WHERE Difference= sales-AVG(sales));
您想要创建一个 table 包含三列的结果:title
、author
和 Difference
(其销售额与平均销售额之间的差异)。在 sql 中,您可以在 select 表达式中进行数学运算,因此您只需将 (sales-AVG(sales))
添加到列列表中即可。要指定名称,您可以使用关键字 AS
.
SELECT title, author, (sales-AVG(sales)) AS Difference FROM book WHERE sales>AVG(sales)