Mysql数据库中sql语句常见的面试题和例子
Sql语句 常见实例 以及解决办法;
基于索引的SQL语句优化
常见的Mysql面试题
操作方法
- 01
SQL 中LEFT JOIN的含义是__,如果 tbl_user记录了学生的姓名(name)和学号(ID), tbl_score记录了学生(有的学生考试以后被 开除了,没有其记录)的学号(ID)和考试成绩(score)以及考试科目(subject),要想打印出各个学生姓名及对应的的各科总成绩,则可以用 SQL语句____. 答:自然左外连接 create database phpinterview; use phpinterview create table tbl_user ( ID int not null, name varchar(50) not null, primary key (ID) ); create table tbl_score ( ID int not null, score dec(6,2) not null, subject varchar(20) not null ); insert into tbl_user (ID, name) values (1, ’beimu’); insert into tbl_user (ID, name) values (2, ’aihui’); insert into tbl_score (ID, score, subject) values (1, 90, ’语文’); insert into tbl_score (ID, score, subject) values (1, 80, ’数学’); insert into tbl_score (ID, score, subject) values (2, 86, ’数学’); insert into tbl_score (ID, score, subject) values (2, 96, ’语文’); select A.id,sum(B.score) as sumscore from tbl_user A left join tbl_score B on A.ID=B.ID group by A.id
- 02
计选修课的学生总数和考试的平均成绩。SELECT COUNT(DISTINCT(Sno)) AS 学生总数,AVG(Grade) AS 平均成绩FROM SC --//用DISTINCT消除重复的行
- 03
查询选课门数超过2门的学生的平均成绩和选课门数。SELECT Student.Sname,AVG(Sc.Grade) 平均成绩,COUNT(SC.Sno) 选课门数FROM SC --内联接join或inner join,内联系是比较运算符,只返回符合条件的行 JOIN Student ON (SC.Sno = Student.Sno) JOIN Course ON (SC.Cno = Course.Cno)GROUP BY Student.SnameHAVING COUNT(distinct Course.Cno) >2--分组条件