-- 查询练习 1------------ CREATE TABLE stu (sid CHAR ( 6 ),sname VARCHAR ( 50 ),age INT ,gender VARCHAR ( 50 )); INSERT INTO stu VALUES ( ‘ S_1001 ‘ , ‘ liuYi ‘ , 35 , ‘ male ‘ ); INSERT INTO stu VALUES ( ‘ S_1002 ‘ , ‘
-- 查询练习 1------------
CREATE TABLE stu ( sid CHAR(6), sname VARCHAR(50), age INT, gender VARCHAR(50) ); INSERT INTO stu VALUES(‘S_1001‘, ‘liuYi‘, 35, ‘male‘); INSERT INTO stu VALUES(‘S_1002‘, ‘chenEr‘, 15, ‘female‘); INSERT INTO stu VALUES(‘S_1003‘, ‘zhangSan‘, 95, ‘male‘); INSERT INTO stu VALUES(‘S_1004‘, ‘liSi‘, 65, ‘female‘); INSERT INTO stu VALUES(‘S_1005‘, ‘wangWu‘, 55, ‘male‘); INSERT INTO stu VALUES(‘S_1006‘, ‘zhaoLiu‘, 75, ‘female‘); INSERT INTO stu VALUES(‘S_1007‘, ‘sunQi‘, 25, ‘male‘); INSERT INTO stu VALUES(‘S_1008‘, ‘zhouBa‘, 45, ‘female‘); INSERT INTO stu VALUES(‘S_1009‘, ‘wuJiu‘, 85, ‘male‘); INSERT INTO stu VALUES(‘S_1010‘, ‘zhengShi‘, 5, ‘female‘); INSERT INTO stu VALUES(‘S_1011‘, ‘xxx‘, NULL, NULL);
----------查询性别为女,并且年龄大于等于50的记录----------
SELECT * FROM stu WHERE gender=‘female‘ AND age>=50
----------查询学号为S_1001,或者姓名为liSi的记录----------
SELECT * FROM stu WHERE sid=‘S_1001‘ OR sname=‘liSi‘
-- 查询学号为S_1001,S_1002,S_1003的记录
SELECT * FROM stu WHERE sid=‘S_1001‘ OR sid=‘S_1002‘ OR sid=‘S_1003‘
-- 查询学号不是S_1001,S_1002,S_1003的记录
SELECT * FROM stu WHERE NOT (sid=‘S_1001‘ OR sid=‘S_1002‘ OR sid=‘S_1003‘)
-- 查询年龄为null的记录
SELECT * FROM stu WHERE age IS NULL
-- 查询年龄在20到40之间的学生记录
SELECT * FROM stu WHERE age BETWEEN 20 AND 40
-- 查询性别非男的学生记录
SELECT * FROM stu WHERE gend er!=‘male‘
-- 查询姓名不为null的学生记录
SELECT * FROM stu WHERE NOT sname IS NULL
-- 查询姓名由5个字母构成的学生记录
SELECT * FROM stu WHERE sname LIKE ‘_____‘
-- 查询姓名由5个字母构成,并且第5个字母为“i”的学生记录
SELECT * FROM stu WHERE sname LIKE ‘____i‘
-- 查询姓名以“z”开头的学生记录
SELECT * FROM stu WHERE sname LIKE ‘z%‘
-- 查询姓名中第2个字母为“i”的学生记录
SELECT * FROM stu WHERE sname LIKE ‘_i%‘
-- 查询姓名中包含“a”字母的学生记录
SELECT * FROM stu WHERE sname LIKE ‘%a%‘