Toán tử so sánh trong SQL
Bảng theo dõi bảng SINHVIEN có các bản ghi sau:
SQL> SELECT * FROM SINHVIEN; +----+---------+-----+-----------+--------+ | ID | TEN | TUOI| KHOAHOC | HOCPHI | +----+---------+-----+-----------+---------+ | 1 | Hoang | 21 | CNTT | 4 | | 2 | Viet | 19 | DTVT | 3.0 | | 3 | Thanh | 18 | KTDN | 4 | | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+---------+-----+-----------+---------+ 5 rows in set (0.00 sec)
Dưới đây là ví dụ sử dụng toán tử so sánh trong SQL:
SQL> SELECT * FROM SINHVIEN WHERE HOCPHI > 4; +----+----------+-----+---------+----------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+---------+----------+ | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+---------+-----+-----------+---------+ 2 rows in set (0.00 sec) SQL> SELECT * FROM SINHVIEN WHERE HOCPHI = 4; +----+---------+-----+-----------+---------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+---------+-----+-----------+---------+ | 1 | Hoang | 21 | CNTT | 4 | | 3 | Thanh | 18 | KTDN | 4 | +----+---------+-----+-----------+---------+ 2 rows in set (0.00 sec) SQL> SELECT * FROM SINHVIEN WHERE HOCPHI != 4; +----+----------+-----+---------+----------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+---------+----------+ | 2 | Viet | 19 | DTVT | 3.0 | | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+----------+-----+---------+----------+ 3 rows in set (0.00 sec) SQL> SELECT * FROM SINHVIEN WHERE HOCPHI <> 4; +----+----------+-----+---------+----------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+---------+----------+ | 2 | Viet | 19 | DTVT | 3.0 | | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+----------+-----+---------+----------+ 3 rows in set (0.00 sec) SQL> SELECT * FROM SINHVIEN WHERE HOCPHI >= 4.5; +----+----------+-----+---------+----------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+---------+----------+ | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+---------+-----+-----------+---------+ 2 rows in set (0.00 sec)