การสืบค้นด้วย SQL
แนะนำใหม่| English | ความหมาย |
|---|---|
| SQL/ˌes kjuː ˈel/ | SQL (ภาษาสำหรับคำถามเชิงโครงสร้าง) |
| JOIN/dʒɔɪn/ | เชื่อมต่อ |
| GROUP BY/ɡruːp baɪ/ | จัดกลุ่มตาม |
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 ที่ไม่มีข้อจำกัดจะจับคู่รายวิชาสามรายกับการลงทะเบียนสามรายการ จะสามารถสร้างคู่ได้ทั้งหมดกี่คู่?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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 ได้อย่างถูกต้อง?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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;
Keep empty courses and count matches
Separate LEFT JOIN, COUNT and GROUP BY using the lesson’s three courses.
เติม clause ที่ใช้ GROUP BY ตามตัวตนและชื่อรายวิชา: ____ c.id, c.title
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
GROUP BY กำหนดกลุ่ม การรวม ID เข้าไปช่วยให้รายวิชาที่มีชื่อเหมือนกันแต่ต่างกันถูกแยกออกจากกัน
Query ที่สมบูรณ์จะคืนค่าจำนวนการลงทะเบียนสำหรับ Course ID 7, 8 และ 9
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวอย่างมีจับคู่กับ 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 ใดที่กรองกลุ่มเพื่อเหลือเฉพาะกลุ่มที่มีนักเรียนลงทะเบียนอย่างน้อยสองคน?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
HAVING ใช้เงื่อนไขกับกลุ่ม; WHERE กรองแถวข้อมูลเข้า.
อธิบาย WHERE และ HAVING โดยใช้ตัวอย่างนับจำนวนรายวิชา
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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 ถูกต้อง
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เปรียบเทียบจำนวนแถวที่คาดหวัง Known รวมถึงกรณีไม่มีผลและชื่อซ้ำ
การจัดกลุ่มตามเฉพาะชื่อนิยามจะทำให้รวมรายวิชาที่มีชื่อนิยามเดียวกันเข้าด้วยกัน
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ถูกต้อง ชื่อนิยามเท่ากันจะสร้างกลุ่มเดียว ดังนั้นรายวิชา Robotics ทั้งสองจะถูกนับรวมเป็นกลุ่มเดียว; ควรจัดกลุ่มตามเอกลักษณ์ของรายวิชาเพิ่มเติม