Databases & SQL · ฐานข้อมูล & SQL
A database stores tables
- A database keeps data in tables, like a spreadsheet.
- Each row is a record; each column is a field.
- We ask questions in a language called SQL.
ฐานข้อมูลเก็บตาราง
- ฐานข้อมูล เก็บข้อมูลใน ตาราง เหมือนกับสเปรดชีต
- แต่ละแถวคือ บันทึก; แต่ละคอลัมน์คือ ฟิลด์
- เราตั้งคำถามด้วยภาษาที่เรียกว่า SQL
SELECT: choose columns
SELECTpicks which columns to show.FROMnames the table to read.*means "all columns".
SELECT: เลือกคอลัมน์
SELECTเลือก คอลัมน์ ที่ต้องการแสดงFROMระบุชื่อตารางที่ต้องการอ่าน*หมายความว่า "ทุกคอลัมน์"
SELECT name, grade FROM student;
WHERE: filter rows
WHEREkeeps only the rows whose condition is true.- Put text in single quotes:
'A'.
WHERE: กรองแถว
WHEREคงไว้เฉพาะแถวที่มีเงื่อนไขเป็น จริง- ใส่ข้อความใน อัญลักษณ์:
'A'
SELECT … WHERE, visualized · SELECT … WHERE, แสดงภาพ
WHERE keeps only matching rows; SELECT then keeps only the chosen columns. · WHERE จะเก็บเฉพาะแถวที่ตรงเงื่อนไข; SELECT จากนั้นจะเก็บเฉพาะคอลัมน์ที่เลือกไว้เท่านั้น
SELECT name FROM student WHERE grade = 'A';
ORDER BY: sort the results
ORDER BYsorts the rows by a column.- Add
DESCfor largest first, orASC(the default) for smallest first.
ORDER BY: เรียงลำดับผลลัพธ์
ORDER BYเรียงแถวตามคอลัมน์หนึ่ง- เพิ่ม
DESCสำหรับเรียงจากมากไปน้อย, หรือASC(ค่าเริ่มต้น) สำหรับเรียงจากน้อยไปมาก
SELECT name, age FROM student ORDER BY age DESC;
In the exam: SQL
- IGCSE uses exactly these clauses:
SELECT … FROM … WHERE … ORDER BY.
ในการสอบ: SQL
- IGCSE ใช้คลáusเหล่านี้เป๊ะๆ:
SELECT … FROM … WHERE … ORDER BY
SELECT Name, Grade
FROM Student
WHERE Grade = 'A';
Common mistakes
- Text values go in single quotes:
WHERE name = 'Ann'. SELECTchooses the columns;WHEREfilters the rows.
ข้อผิดพลาดที่พบบ่อย
- ค่าข้อความใส่ในอัญลักษณ์:
WHERE name = 'Ann' SELECTเลือกคอลัมน์;WHEREกรองแถว
Now you try
- Write one SQL query for each task.
- Press Check answer to run it against the table.
ลองดูเลย
- เขียนคำสั่ง SQL หนึ่งคำสั่งต่อแต่ละงาน
- กด Check answer เพื่อดำเนินการเทียบกับตาราง
Show the name of every student. (Use SELECT name FROM student;.) · แสดง name ของ ทุก นักเรียน (ใช้ SELECT name FROM student;)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Show the name and grade of every student whose grade is A. (Text goes in single quotes: 'A'.) · แสดง name และ grade ของนักเรียนทุกคนwhose grade เป็น A (ข้อความต้องใส่ในเครื่องหมายอแปส: 'A')
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Show the name of every student, oldest first (sort by age in descending order). · แสดง name ของนักเรียนทุกคน คน oldest ก่อน (เรียงตาม age แบบลดลง)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่