Mental schema for SQL queries [closed]

While preparing SQL introductory material, I’ve ended asking myself about the line of thought followed by a developer when writing a query. I believe it could be too valuable from a beginner’s point of view.

Let me illustrate the subject with an example. For the sake of brevity, I won’t include the database schema, hoping for the query to be descriptive enough by itself.

Suppose we want to write a query for retrieving all new customers from the last quarter. Typical. Me, the first thing I think about is the set of entities involved, beginning with the ones corresponding to the requested data. So, as I want to get customers, first of all I write the following query skeleton:

    SELECT
      FROM Customers AS NewCustomer
     WHERE 

There immediately follow the remaining entities. In this fictional context, a new customer is any customer having placed an order for the first time:

    SELECT
      FROM Customers AS NewCustomer
INNER JOIN Orders AS RecentOrder
 LEFT JOIN Orders AS OlderOrder
     WHERE 
       AND OlderOrder.Id IS NULL

With all the entities identified, I proceed writing the relations between them:

    SELECT
      FROM Customers AS NewCustomer
INNER JOIN Orders AS RecentOrder
        ON RecentOrder.CustomerId = NewCustomer.Id
 LEFT JOIN Orders AS OlderOrder
        ON OlderOrder.CustomerId = RecentOrder.CustomerId
       AND OlderOrder.PlacementDate < RecentOrder.PlacementDate
     WHERE 
       AND OlderOrder.Id IS NULL

Once I’m finished with the entities, it’s time for me to define whatever restrictions to be applied over the resulting data set:

    SELECT 
      FROM Customers AS NewCustomer
INNER JOIN Orders AS RecentOrder
        ON RecentOrder.CustomerId = NewCustomer.Id
 LEFT JOIN Orders AS OlderOrder
        ON OlderOrder.CustomerId = RecentOrder.CustomerId
       AND OlderOrder.PlacementDate < RecentOrder.PlacementDate
     WHERE RecentOrder.PlacementDate BETWEEN @fromDate AND @toDate
       AND OlderOrder.PlacementDate < @fromDate
       AND OlderOrder.Id IS NULL

Finally, I ask myself which concrete data fields I want the query to return, thus completing the SELECT clause:

    SELECT NewCustomer.FirstName, NewCustomer.LastName
      FROM Customers AS NewCustomer
INNER JOIN Orders AS RecentOrder
        ON RecentOrder.CustomerId = NewCustomer.Id
 LEFT JOIN Orders AS OlderOrder
        ON OlderOrder.CustomerId = RecentOrder.CustomerId
       AND OlderOrder.PlacementDate < RecentOrder.PlacementDate
     WHERE RecentOrder.PlacementDate BETWEEN @fromDate AND @toDate
       AND OlderOrder.PlacementDate < @fromDate
       AND OlderOrder.Id IS NULL

Of course, this is a very simple query, without aggregates, nested queries and other advanced stuff. But this is my overall train of thought, whatever the query’s complexity:

  1. Entities
  2. Relations
  3. Restrictions
  4. Fields

My question is, is this a suitable schema to be taught explicitly to a beginner? How could it be improved, anyway, in order to smooth her learning curve?

tl;dr

I think you have it exactly backwards.


A knowledgeable developer constructs his queries in the order you mention there because that is the order of dependencies – You don’t know what relations you need to follow until you know what entities you need, you don’t know what restrictions are necessary without first identifying your relations, and you can’t know what to restrict on until you know the field set at your disposal.

Therefore a knowledgeable developer often times follows the order as set forth because in the real world we need the highest level component – the entity – and that part drives how we use all the others.

However, this order is from highest level most dependant to lowest level least dependant pieces. For someone who’s unfamiliar with SQL you don’t want to teach entities first when they don’t yet know about relations, restrictions, or fields; transitively you shouldn’t teach relations to someone who doesn’t know restrictions or fields, or restrictions to someone who doesn’t know fields.

Given the concept you’re detailing above and the break down the way I see it, you should teach them in precisely the opposite order as you have suggested.

I think this becomes obvious if you look at the example query you have above – to query for conceptual entities you end up with a query far more complex than a new SQL developer should have to look at first. Further, until the last piece you haven’t got a completed SQL query because your last thing to teach is the most dependent piece that every other piece requires just to work at all. The way you suggest going about it would have you giving incomplete SQL examples as you have above until you were done teaching all of the concepts, it seems more reasonable to give smaller complete examples that grow more complex rather than growing more complete.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật