การสืบค้นด้วย SQL
| English | ไทย |
|---|---|
| SQL/ˌes kjuː ˈel/ | SQL |
| JOIN/dʒɔɪn/ | JOIN |
| GROUP BY/ɡruːp baɪ/ | GROUP BY |
Two different courses share a title
- Two courses are both called Robotics. Grouping only by title combines their enrolments and hides that they are different courses.
- SQL 结构化查询语言 describes the data result you want. Check row identity and expected output before trusting a plausible count.
Follow the relationship in the join
- A JOIN 连接 combines rows using a relationship such as
e.course_id = c.id. Write the intended relationship explicitly. - A cross join pairs every row on one side with every row on the other. In SQLite, an unconstrained join can produce that product; other SQL systems can reject missing join conditions.
ใน SQLite การ join ที่ไม่มีข้อจำกัดจะจับคู่รายวิชาสามรายกับการลงทะเบียนสามรายการ จะสามารถสร้างคู่ได้ทั้งหมดกี่คู่?
Cross product มี 3 × 3 แถว ก่อนการกรองใดๆ ในภายหลัง
Keep zero-enrolment courses
- A left join keeps every course, including one without a matching enrolment. Its unmatched enrolment fields are null.
- Count
e.student_id, which excludes null, rather thanCOUNT(*), which also counts the retained unmatched row. The empty course should report zero.
Expression ใดนับการลงทะเบียนเป็นศูนย์สำหรับรายวิชาที่ไม่ได้จับคู่ผ่าน left-join ได้อย่างถูกต้อง?
COUNT ของฟิลด์ nullable ที่ไม่ได้จับคู่จะตัด null ออก; COUNT(*) นับแถวที่ถูกคงไว้
Run a query with known answers
- Run this complete script in a local SQLite database. Its two Robotics courses have different IDs, and Art has no enrolments.
- GROUP BY 分组 groups by both course ID and title. Expected rows are
(7, Robotics, 1),(8, Robotics, 2)and(9, Art, 0).
CREATE TABLE courses (id INTEGER PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE enrolments (student_id INTEGER, course_id INTEGER);
INSERT INTO courses VALUES (7, 'Robotics'), (8, 'Robotics'), (9, 'Art');
INSERT INTO enrolments VALUES (1, 7), (1, 8), (2, 8);
SELECT c.id, c.title, COUNT(e.student_id) AS enrolled
FROM courses AS c
LEFT JOIN enrolments AS e ON e.course_id = c.id
GROUP BY c.id, c.title
ORDER BY c.id;
เติม clause ที่ใช้ GROUP BY ตามตัวตนและชื่อรายวิชา: ____ c.id, c.title
GROUP BY กำหนดกลุ่ม การรวม ID เข้าไปช่วยให้รายวิชาที่มีชื่อเหมือนกันแต่ต่างกันถูกแยกออกจากกัน
Query ที่สมบูรณ์จะคืนค่าจำนวนการลงทะเบียนสำหรับ Course ID 7, 8 และ 9
ตัวอย่างมีจับคู่กับ 7 จำนวนหนึ่ง, 8 สอง และไม่มีสำหรับ 9
Filter rows and groups at different stages
WHEREfilters input rows before grouping.HAVINGfilters groups using a condition such asCOUNT(e.student_id) >= 2.- In this example, adding that HAVING condition before ORDER BY returns only course 8. Filtering
e.student_idin WHERE can remove the unmatched rows you meant to retain.
Clause ใดที่กรองกลุ่มเพื่อเหลือเฉพาะกลุ่มที่มีนักเรียนลงทะเบียนอย่างน้อยสองคน?
HAVING ใช้เงื่อนไขกับกลุ่ม; WHERE กรองแถวข้อมูลเข้า.
อธิบาย WHERE และ HAVING โดยใช้ตัวอย่างนับจำนวนรายวิชา
WHERE กรองแถว; HAVING ทดสอบจำนวนของแต่ละกลุ่ม
Use edge cases to verify the result
- Check repeated titles, zero matches and multiple matches against known counts. A result that looks reasonable is not enough.
- Explain the selected fields, join condition, grouping and count. Decide whether duplicate relationship rows are valid or should be prevented by the schema.
Join on the relationship, group by the identity, and count the intended thing. A tiny known dataset makes incorrect queries easier to see.
จำนวนที่Logsically เป็นไปได้เพียงอย่างเดียวเป็นพยานเพียงพอว่า SQL Query ถูกต้อง
เปรียบเทียบจำนวนแถวที่คาดหวัง Known รวมถึงกรณีไม่มีผลและชื่อซ้ำ
การจัดกลุ่มโดยชื่อเรื่องเพียงอย่างเดียวทำให้รายวิชาหุ่นยนต์ทั้งสองแยกออกจากกัน
ชื่อเรื่องเท่ากันจะรวมอยู่ในกลุ่มเดียวกัน ควรจัดกลุ่มตามตัวตนของรายวิชาเพิ่มเติมด้วย