Cấp độ: intermediate40 phútminMiễn phíFreeProduct DeveloperProduct Developer

Database: SQL cơ bảnDatabase: Basic SQL

Database sinh viên với query thống kêA student database with statistical queries

Bạn sẽ học gìWhat you will learn

  • Tạo bảng với CREATE TABLECreate tables with CREATE TABLE
  • Thực hiện CRUD với SQLPerform CRUD with SQL
  • Dùng JOIN nối bảngUse JOIN to link tables

Bạn sẽ tạo gìWhat you will build

Database sinh viên với query thống kêA student database with statistical queries

Nội dung bài họcLesson content

Database và SQL là gì?What are databases and SQL?

Database lưu dữ liệu có cấu trúc. SQL (Structured Query Language) là ngôn ngữ giao tiếp với database quan hệ. SQLite là database nhẹ, miễn phí, lưu trong 1 file — lý tưởng cho người mới bắt đầu. Cài: npm install sqlite3.A database stores structured data. SQL (Structured Query Language) is the language for communicating with relational databases. SQLite is a lightweight, free database stored in a single file — ideal for beginners. Install: npm install sqlite3.

CREATE TABLE: tạo bảngCREATE TABLE: creating tables

Bảng (table) là cấu trúc lưu dữ liệu dạng hàng-cột. CREATE TABLE định nghĩa tên bảng, các cột, kiểu dữ liệu. PRIMARY KEY là cột định danh duy nhất. AUTOINCREMENT tự tăng. NOT NULL yêu cầu có giá trị.A table is a structure that stores data in rows and columns. CREATE TABLE defines the table name, columns, and data types. PRIMARY KEY is a unique identifier column. AUTOINCREMENT auto-increments. NOT NULL requires a value.

sql
                      
                        CREATE TABLE students (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  age INTEGER,
  score REAL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Các kiểu dữ liệu phổ biến:
-- INTEGER: số nguyên
-- TEXT: chuỗi văn bản
-- REAL: số thập phân
-- DATETIME: ngày giờ
                      
                    

INSERT, SELECT, WHEREINSERT, SELECT, WHERE

INSERT INTO thêm hàng mới. SELECT truy vấn dữ liệu. WHERE lọc điều kiện. ORDER BY sắp xếp. LIMIT giới hạn số hàng. Đây là 4 lệnh SQL dùng nhiều nhất.INSERT INTO adds a new row. SELECT queries data. WHERE filters by condition. ORDER BY sorts. LIMIT restricts row count. These are the 4 most-used SQL commands.

sql
                      
                        -- Thêm dữ liệu
INSERT INTO students (name, age, score)
VALUES ('Hà Mi', 18, 9.5);

INSERT INTO students (name, age, score)
VALUES ('An', 17, 8.0);

-- Truy vấn tất cả
SELECT * FROM students;

-- Truy vấn có điều kiện
SELECT name, score FROM students
WHERE score >= 8.0
ORDER BY score DESC
LIMIT 10;
                      
                    

JOIN: nối nhiều bảngJOIN: linking multiple tables

JOIN nối 2 bảng theo cột chung. INNER JOIN chỉ lấy hàng khớp ở cả 2 bảng. LEFT JOIN lấy tất cả hàng bên trái + hàng khớp bên phải. Ví dụ: JOIN students với classes qua class_id để xem học sinh thuộc lớp nào.JOIN links 2 tables by a common column. INNER JOIN only takes matching rows from both tables. LEFT JOIN takes all left rows + matching right rows. Example: JOIN students with classes via class_id to see which class each student belongs to.

sql
                      
                        -- Tạo bảng classes
CREATE TABLE classes (
  id INTEGER PRIMARY KEY,
  name TEXT
);

-- Thêm cột class_id vào students
ALTER TABLE students ADD COLUMN class_id INTEGER;

-- JOIN: xem học sinh cùng tên lớp
SELECT students.name, students.score, classes.name AS class_name
FROM students
INNER JOIN classes ON students.class_id = classes.id
WHERE students.score >= 8.0;
                      
                    

Bài tập thực hànhHands-on exercises

1Tạo bảng products với cột: id (PK auto), name (TEXT), price (REAL), stock (INTEGER). Thêm 3 sản phẩm.Create a products table with columns: id (PK auto), name (TEXT), price (REAL), stock (INTEGER). Add 3 products.

Gợi ýHint

CREATE TABLE products (...) → INSERT INTO products 3 lần.CREATE TABLE products (...) → INSERT INTO products 3 times.

Lời giảiSolution
sql
                      
                        CREATE TABLE products (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  price REAL,
  stock INTEGER
);

INSERT INTO products (name, price, stock) VALUES ('Laptop', 1500.00, 10);
INSERT INTO products (name, price, stock) VALUES ('Mouse', 25.50, 50);
INSERT INTO products (name, price, stock) VALUES ('Keyboard', 45.00, 30);
                      
                    

2Truy vấn tất cả sản phẩm có giá > 30, sắp xếp giảm dần theo giá.Query all products with price > 30, sorted by price descending.

Gợi ýHint

SELECT * FROM products WHERE price > 30 ORDER BY price DESCSELECT * FROM products WHERE price > 30 ORDER BY price DESC

Lời giảiSolution
sql
                      
                        SELECT * FROM products
WHERE price > 30
ORDER BY price DESC;
-- Kết quả: Laptop (1500), Keyboard (45)
                      
                    

Kiểm traQuiz

Chọn đáp án đúng cho mỗi câu. Nhấn để xem giải thích.Choose the correct answer for each question. Click to see explanation.

Câu 1: SQL là gì?Q1: What is SQL?

Câu 2: Lệnh nào thêm hàng mới vào bảng?Q2: Which command adds a new row to a table?

Câu 3: PRIMARY KEY là gì?Q3: What is PRIMARY KEY?

Câu 4: INNER JOIN làm gì?Q4: What does INNER JOIN do?

Code mẫuSample code

sql
                -- Tạo bảng / Create table
CREATE TABLE students (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  age INTEGER,
  score REAL
);

-- Thêm dữ liệu / Insert data
INSERT INTO students (name, age, score)
VALUES ('Hà Mi', 18, 9.5);

-- Truy vấn / Query
SELECT name, score
FROM students
WHERE score >= 8.0
ORDER BY score DESC;