First day of SQL learning-SQL exercises (Table creation/SQL statements)

Source: Internet
Author: User

Question: consumers from Madrid who have less than 3 orders

Table creation:
Copy codeThe Code is as follows:
Set nocount on -- when set nocount is ON, no count is returned (the number of rows affected by the Transact-SQL statement ). When set nocount is OFF, return count
Use SY
GO
If object_Id ('dbo. Orders ') is not null
Drop table dbo. Orders
GO
If object_Id ('dbo. customer') is not null
Drop table dbo. MERs
GO
Create table dbo. MERs
(
Customerid char (5) not null primary key,
City varchar (10) not null
);
Insert into dbo. MERs values ('fissa ', 'porter ');
Insert into dbo. MERs values ('frndo', 'porter ');
Insert into dbo. MERs values ('krlos ', 'porter ');
Insert into dbo. MERs values ('mrphs', 'zion ');

Create table dbo. Orders
(
Orderid int not null primary key,
Customerid char (5) null references MERs (customerid)
)
Insert into dbo. Orders values (1, 'frndo ');
Insert into dbo. Orders values (2, 'frndo ');
Insert into dbo. Orders values (3, 'krlos ');
Insert into dbo. Orders values (4, 'krlos ');
Insert into dbo. Orders values (5, 'krlos ');
Insert into dbo. Orders values (6, 'mrphs ');
Insert into dbo. Orders values (7, null );

Bytes ------------------------------------------------------------------------------------------------------------------------------

Question Analysis:
Copy codeThe Code is as follows:
Select customerid as consumer, count (customerid) as Order Number
From dbo. Orders
Where customerid in (
Select customerid
From dbo. Customers
Where city = 'Madrid ')
Group by customerid
Having count (customerid) <3

Result:

-- The first thought of the answer suddenly found that a FISSA order from Madrid was missing, and the number of FISSA Orders was 0, so it was not displayed in the Orders table, so the writing method above would be one less.

-- Overturned the above answer, and thought of using Table connections, and the use of internal connections will be the same as the above, so I chose the left join, as shown below:
Copy codeThe Code is as follows:
Select C. customerid as consumer, count (O. customerid) as Order Number
From dbo. Customers as C left join dbo. Orders as O on C. customerid = O. customerid
Where C. city = 'Madrid'
Group by C. customerid
Having count (C. customerid) <3

Result:

-- The query result is correct.

-- Analyze and view left connections without conditions
Copy codeThe Code is as follows:
Select * from dbo. Customers as C left join dbo. Orders as O on C. customerid = O. customerid
Copy codeThe Code is as follows:
Select * from dbo. Customers as C left join dbo. Orders as O on C. customerid = O. customerid

Result:

-- The standard answer in the book is:
Copy codeThe Code is as follows:
Select C. customerid, count (O. orderid) as numorders
From dbo. Customers as C left join dbo. Orders as O on C. customerid = O. customerid
Where C. city = 'Madrid'
Group by C. customerid
Having count (O. orderid) <3
Order by numorders

Result:

--The book only gives an additional order by to define the sorting method (sort by the ascending order of the numorders column)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.