由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - sql question
相关主题
再请教SQL问题SQL combine two columns from two different tables no shared columns
请帮忙作个SQL的题目问一个面试题: sql中的 inner join 和 outer join的区别?
请帮忙做一个SQL问题SQL copy a table into a new table and add a new column (转载)
发个面试题SQL 面试问题
请问这个查询如何用sql join实现? (转载)请教个SQL的问题
问个sql小提。大牛勿进这题怎么做
一道sqlSQL 面试题 - 请高手指点
Two Intern Positions - 请帮转发请教SQL问题
相关话题的讨论汇总
话题: customers话题: customer话题: orders话题: table话题: order
进入JobHunting版参与讨论
1 (共1页)
a******u
发帖数: 239
1
Question:
please, as simply as possible, design a single table to hold orders made by
customers. You may assume details (including names) about customers are
held in some other table. Using the table that you designed for orders, and
this other table, present a single query that gives the number of orders
for every customer, one line per customer.
My answer:
1. table design
Table: Orders
order_id Long int Primary key (auto increase)
customer_id Long int Foreign key
order_date date
… (other information about the order)
Table: Customers
customer_id Long int Primary key (auto increase)
customer_name string
… (other information about the customer)
2. SQL statement
SELECT Customers. customer_name, COUNT(Orders. order_id) AS Ordernum
FROM Customers
INNER JOIN Orders
ON Customers. customer_id =Orders. customer_id
ORDER BY Customers. customer_name
However, they said my answer is not correct, why?
Thank you very much!
b***e
发帖数: 383
2
ORDER BY Customers. customer_name
应该用group by customer_id, 在select语句 那里也把 customer_id 加进去。
a9
发帖数: 21638
3
至少得有个给customer的订单号吧?

by
and

【在 a******u 的大作中提到】
: Question:
: please, as simply as possible, design a single table to hold orders made by
: customers. You may assume details (including names) about customers are
: held in some other table. Using the table that you designed for orders, and
: this other table, present a single query that gives the number of orders
: for every customer, one line per customer.
: My answer:
: 1. table design
: Table: Orders
: order_id Long int Primary key (auto increase)

a******u
发帖数: 239
4
So, it should be:
SELECT Customers. customer_name, COUNT(Orders. order_id) AS Ordernum
FROM Customers
WHERE Customers.customer_name = “customername”
INNER JOIN Orders
ON Customers. customer_id =Orders. customer_id
GROUP BY Customers. customer_id
Now I understand why I should use "Group By".
Thank you very much.
r****t
发帖数: 10904
5
1. order by 肯定不行的,必须 group by
2. on 是不是改成 where 才对?
3. inner join 我没学过,按 sql 标准是直接 from customers, orders,问题里面有要求是啥实现没有?

by
and

【在 a******u 的大作中提到】
: Question:
: please, as simply as possible, design a single table to hold orders made by
: customers. You may assume details (including names) about customers are
: held in some other table. Using the table that you designed for orders, and
: this other table, present a single query that gives the number of orders
: for every customer, one line per customer.
: My answer:
: 1. table design
: Table: Orders
: order_id Long int Primary key (auto increase)

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教SQL问题请问这个查询如何用sql join实现? (转载)
hash table 能找到median吗?问个sql小提。大牛勿进
问一个数据库的题 (转载)一道sql
求教一个SQL的问题Two Intern Positions - 请帮转发
再请教SQL问题SQL combine two columns from two different tables no shared columns
请帮忙作个SQL的题目问一个面试题: sql中的 inner join 和 outer join的区别?
请帮忙做一个SQL问题SQL copy a table into a new table and add a new column (转载)
发个面试题SQL 面试问题
相关话题的讨论汇总
话题: customers话题: customer话题: orders话题: table话题: order