Test your T-SQL basics

Source: Internet
Author: User

Always think of themselves SQL is very good, did not think of today in the refactoring stored procedure encountered a subquery problem, modified to self-connected after the discovery unexpectedly result is not correct, so have the following test. Suppose the table has the following number of data, ask Query1,query2,query3 query results?

Id OrderID Transid Type
1 1 1 E
2 1 1 A
3 1 1 B
4 1 2 A
5 1 2 B
6 2 1 A
7 2 1 B
8 2 2 A

--Query 1SELECT *  fromOrderDetail a Left JoinOrderDetail b onA.orderid=B.orderid andA.transid=B.transid andB.goods='E'WHEREB.goods is  NULL--Query 2SELECT *  fromOrderDetail aWHEREA.transid<>(   SELECT Top 1B.transid fromOrderDetail bWHEREB.orderid=A.orderid andB.goods='E')--Query 3SELECT *  fromOrderDetail aWHERE  not EXISTS (    SELECTB.transid,b.orderid fromOrderDetail bWHEREB.orderid=A.orderid andB.goods='E')

Please write your answers silently before viewing the results of the operation:
Query1

ID    OrderID    transid    Goods4    1    2    A5    1    2    B6    2    1    A7    2    1    B8    2    2    A
View Code

Query2

ID    OrderID    transid    Goods4    1    2    A5    1    2    B
View Code

Query3

ID    OrderID    transid    Goods6    2    1    A7    2    1    B8    2    2    A
View Code

Test your T-SQL basics

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.