Export existing database data:
- mysqldump -u username -p password database name> Export file structure + data path #
- mysqldump -u username -p password -d database name> Export file path structure #
Import an existing database data:
- mysqldump -uroot -p password database name <file path
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost
Source Database : sqlexam
Target Server Type : MySQL
Target Server Version : 50624
File Encoding : utf-8
Date: 10/21/2016 06:46:46 AM
*/
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
- ----------------------------
- Records of `class`
- ----------- -----------------
the BEGIN;
the INSERT the INTO `class` the VALUES ( '. 1', 'second class three'), ( '2', 'three three shifts') , ( '3', 'second class year'), ( '4', 'two years nine classes');
a COMMIT;
-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
- ----------------------------
- Records of `course`
- ----------- -----------------
the BEGIN;
the INSERT the INTO `course` the VALUES ( '1', 'biological', '1'), ( '2', 'physical', '2 '), (' 3 ',' sports', '3'), ( '4', 'art', '2');
a COMMIT;
-- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`num` int(11) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_score_student` (`student_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');
COMMIT;
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` char(1) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_class` (`class_id`),
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
- ----------------------------
- Records of `student`
- ----------- -----------------
the BEGIN;
the INSERT the INTO `student` the VALUES ( '1', 'M', '1', 'understanding'), ( '2', 'F ',' 1 ',' steel egg '), (' 3 ',' M ',' 1 ',' John Doe '), (' 4 ',' M ',' 1 ',' Zhang '), ( '5', 'F', '1', 'Zhang two'), ( '6', 'M', '1', 'ZHANG Si'), ( '7', 'F', '2' 'hammer'), ( '8', 'M', '2', 'Lee III'), ( '9', 'M', '2', 'Lee'), ( '10', 'female', '2', 'Li Er'), ( '11', 'male', '2', 'John Doe'), ( '12', 'female', '3', 'flower') , ( '13', 'M ',' 3 ',' Liu three '), (' 14 ',' M ',' 3 ',' Liu a '), (' 15 ',' F ',' 3 ',' and Liu ') , ( '16', 'M', '3', 'four Liu');
a COMMIT;
-- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
- ----------------------------
- Records of `teacher`
- ----------- -----------------
the BEGIN;
the INSERT the INTO `teacher` the VALUES ( '. 1', 'Lei teacher'), ( '2', 'teacher Li'), ( ' 3 ',' Ms Liu teacher '), (' 4 ',' Zhuyun Hai teacher '), (' 5 ',' Jie teacher ');
a COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
Table data structure and
2, the query "biological" course higher than the "physical" course grade all students learn numbers;
ideas:
Get all human biological programs (school, score) - a temporary table
to get all people have physical courses (student number, results) - temporary tables
connecting two temporary tables according to [number] Studies:
Studies on biological results results physical No.
then screened
SELECT A.student_id, SW, TY from
(SELECT the student_id, SW NUM AS left from the Join Course score score ON. course_id = course.cid where course.cname = 'biological') AS A
left the Join
(SELECT the student_id, NUM AS TY left from the Join Course Score = ON score.course_id course.cid WHERE course.cname = 'sports') AS B
ON SW = B.student_id WHERE A.student_id> IF (ISNULL (TY), 0, TY);
. 3, the average score of the query points is greater than 60 and the average number of students learning results;
ideas:
the student packet, using an average value avg obtain , were screened by having avg
student_id the SELECT, AVG (NUM) from Score Group by student_id the HAVING AVG (NUM)> 60
4, query for all students number, name, number of elective total score;
the SELECT score.student_id, SUM (score.num), COUNT ( score.student_id), student.sname
from
Score left the Join Student = ON score.student_id student.sid
Group by score.student_id
. 5, the query name teacher "Li" of the number;
SELECT COUNT (TID) from teacher tname like WHERE ' Lee% '
the SELECT COUNT (1) from (the SELECT tid from the WHERE teacher tname like' Lee% ') AS B
6, query never learned "flat leaf" class teacher of students number, name;
ideas:
first found " Li Ping teacher "teacher of all class ID
to obtain the election had lessons for all students ID
student table screened
the SELECT * student from the WHERE sid not in (
the SELECT DISTINCT student_id Score from score.course_id in the WHERE (
select cid from course left join teacher on course.teacher_id = teacher.tid where tname = ' Li teacher'
)
)
7, the query learned "001" and I have learned a number "002" students of the course number, name;
ideas:
first found in select both 001 and 002 selected courses all students
are grouped according to the student, if the student is equal to the number 2 indicates, two have selected
the sELECT student_id, sname from
(the sELECT student_id, COURSE_ID from COURSE_ID Score = 1 or the WHERE COURSE_ID 2 =) aS B
left the Join Student ON B.student_id = student.sid Group by student_id the HAVING COUNT (student_id)> 1
8, query all learned lessons "Ye Ping" teachers teach the students the school, name;
ibid. , but the 001 and 002 become in (leaf level teachers all classes)
9, curriculum inquiry number "002" results "001" number is lower than the course curriculum for all students of the school number, name;
with the title of the first
10 inquiries have less than 60 points of the course grade students number, name;
the SELECT sid, sname from Student in the WHERE sid (
Score the WHERE DISTINCT student_id from the SELECT NUM <60
)
11, the query did not learn all the students of the whole class number, name;
ideas:
grouping students according to the score table, get a number of students per enrollment
if the number of the total number of courses == , said that it has selected all the courses
the sELECT student_id, sname
from the Join Student Score left = ON score.student_id student.sid
Group by student_id the HAVING COUNT (COURSE_ID) = (the sELECT COUNT (1) from course,)
12, there is at least one course of inquiry school for the same number of students in school number and name "001," the students have learned;
thinking:
get all 001 students selected courses
get everyone and all courses in the curriculum which
screened for the students, the students get all the information
and then with the student table connection, obtain the name of
the SELECT student_id, sname, COUNT (COURSE_ID)
from the Join Student Score left = ON score.student_id student.sid
! the WHERE student_id = 1 and COURSE_ID in (the SELECT COURSE_ID from Score the WHERE student_id = 1) Group by student_id
13, query least studied school number "001" students of all class of other students to learn numbers and names;
first found and 001 school everyone had
then number = 001 all disciplines == "others may choose the more
the sELECT student_id, sname, COUNT (COURSE_ID)
from the Join Student Score left = ON score.student_id student.sid
the WHERE student_id! = 1 and COURSE_ID in (the SELECT COURSE_ID from the WHERE student_id Score = 1) Group by student_id the HAVING COUNT (COURSE_ID) = (the SELECT COUNT (COURSE_ID) from the WHERE student_id Score = 1)
14, query and "002" number of students learning the same curriculum other school students number and name;
the same number of
002 is also learned learned
select student_id, sname from score left join student on score.student_id = student.sid where student_id in (
the SELECT student_id from Score the WHERE student_id! = 1 Group by student_id the HAVING COUNT (COURSE_ID) = (the SELECT COUNT (1) from Score the WHERE student_id = 1)
) and COURSE_ID in (the SELECT COURSE_ID from Score the WHERE student_id = 1) Group by student_id the HAVING COUNT (COURSE_ID) = (the SELECT COUNT (1) from the WHERE student_id score = 1)
15, delete the score table records study "flat leaf" class teacher;
the delete from score in the WHERE COURSE_ID (
the SELECT cid left from the Join teacher Course, ON course.teacher_id = teacher.tid where teacher.name = 'Epping'
)
16, is inserted into some records SC table, the record meets the following requirements conditions: ① not had number "002" number of courses the students learn; ② insert "002" grade point average number of courses;
thinking:
Since the insert support
inset into tb1 (xx, xx) select x1, x2 from tb2;
all, get all the owners never went to class 002, get an average score of 002
INSERT INTO Score (the student_id, COURSE_ID, NUM) SELECT SID, 2, (SELECT AVG (NUM) from Score WHERE COURSE_ID = 2)
from Student WHERE SID Not in (
SELECT the student_id from Score WHERE COURSE_ID = 2
)
. 17, according to the average score from low-to-high display of all students "language", "Mathematics", "English" three course grade, displayed as follows: student ID, language, mathematics, English, the effective number of courses, the effective average;
the SELECT sc.student_id ,
(SELECT NUM left from the Join Course Score = ON score.course_id course.cid WHERE course.cname = "bio" and score.student_id = sc.student_id) SY AS,
(left SELECT NUM from the Join Course Score = ON score.course_id course.cid where course.cname = "physical" and score.student_id = sc.student_id) as wl ,
(select num from score left join course on score.course_id = course.cid where course.cname = " Sports" and score.student_id = sc.student_id) AS TY,
COUNT (sc.course_id),
AVG (sc.num)
from AS SC score
Group by the student_id desc
18 is, query subjects the highest score and the lowest point: displaying categorized as follows: ID course, the highest score, the lowest score;
SELECT COURSE_ID, max (NUM) AS MAX_NUM, min (NUM) from AS MIN_NUM COURSE_ID by Group score;
. 19, subjects according to the percentage of the average score in descending order from the low and high pass rate;
ideas: Case .. When the then
SELECT COURSE_ID, AVG (NUM) AS avgnum, SUM (Case When score. NUM> 60. 1 the else the then the END 0) / COUNT (. 1) Score * 100 AS from Group Percent Order by avgnum COURSE_ID by ASC, Percent desc;
20 is, Low average course display (real instructor);
select avg(if(isnull(score.num),0,score.num)),teacher.tname from course
left join score on course.cid = score.course_id
left join teacher on course.teacher_id = teacher.tid
group by score.course_id
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join
(
select
sid,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 3,1) as second_num
from
score as s1
) as T
on score.sid =T.sid
score.num the WHERE <= T.first_num and score.num> = T.second_num
22, the number of queries for each course is elective students;
the SELECT COURSE_ID, COUNT (1) Score from Group by COURSE_ID;
23, check out the only elective all students in a course of study number and name;
the SELECT student.sid, student.sname, COUNT (1) Score from
left ON score.student_id = student.sid the Join student
Group COURSE_ID by the HAVING COUNT (1) = 1
24-, the number of inquiries boys, girls;
the SELECT * from
(the SELECT COUNT (1) AS man from Student the WHERE Gender = 'M') AS a,
(the SELECT COUNT (1) AS feman from Student the WHERE Gender = 'female') AS B
25 query surname "Chang" in the list of students;
the SELECT sname from student the WHERE sname like 'Zhang%';
26, query the same name list of students, and the number of statistics of the same name;
the SELECT sname, COUNT (1) AS COUNT from student Group by sname;
27, query the average score of each course, the results of the average results in ascending order, the average score of the same, according to the course number in descending order;
SELECT COURSE_ID, AVG (IF (ISNULL (NUM), 0, NUM)) AS AVG from Score Group by COURSE_ID the Order by AVG asc, COURSE_ID desc;
28, query the average score is greater than all students in 85 school number, name and grade point average;
the SELECT student_id, sname, AVG (IF (ISNULL (NUM), 0, NUM)) from score the Join student score.student_id = ON left student.sid Group by student_id;
29, the query name for the course "Mathematics" and scores below the student's name and score of 60;
the SELECT student.sname, score.num score from
left the Join course, ON = course.cid score.course_id
left the Join student oN score.student_id = student.sid
the WHERE score.num <= 60 and course.cname 'biological'
30, details of the curriculum and courses numbered 003 students in grades 80 points or more in school number and name;
the SELECT * from the WHERE score.student_id Score = 3 and score.num> 80
31, seeking the number of students chose courses
COUNT the SELECT (DISTINCT student_id) Score from
the SELECT COUNT (c) from (
the SELECT COUNT (student_id) AS c Score from Group by student_id) AS A
32, student inquiry elective "Yang Yan" teacher grant program, the highest-achieving students name and performance;
the SELECT sname, NUM score from
left score.student_id the Join Student ON = student.sid
the WHERE score.course_id in (the SELECT course.cid from the Join Teacher Course, left the WHERE ON course.teacher_id = teacher.tid tname = 'Zhang Lei the teacher ') Order by NUM desc limit. 1;
33 is, each query corresponding elective courses and the number;
SELECT course.cname, COUNT (. 1) Score from
left ON score.course_id = course.cid the Join course
Group by COURSE_ID;
34 is, the query different courses but the same number of students' learning achievement, curriculum, student achievement;
DISTINCT s1.course_id the SELECT, s2.course_id, s1.num, s2.num Score from AS s1, s2 the WHERE AS s1.num Score = s2.num and s1.course_id = s2.course_id;!
35, the query results for each course preferably the top two;
SELECT score.sid, score.course_id, score.num, T.first_num, T.second_num Score left from the Join
(
SELECT
SID,
(AS SELECT NUM from Score = S2 WHERE s2.course_id s1.course_id NUM desc limit by 0,1 Order) AS FIRST_NUM,
(S2 AS SELECT NUM Score from WHERE s2.course_id NUM = s1.course_id Order by desc limit 1,1) AS second_num
from
Score S1 AS
) AS T
ON = score.sid T.sid
WHERE score.num <= T.first_num and score.num> = T.second_num
36, retrieving at least two courses elective student number;
the SELECT student_id 1 from Score Group by student_id the HAVING COUNT (student_id)>
37, details of the curriculum of all students elective course number and course name;
the SELECT COURSE_ID, COUNT (1) from Score Group by COURSE_ID the HAVING COUNT (1) = (the SELECT COUNT (1) from student);
38, never learned inquiry "Ye Ping" student name any one course teacher taught;
the SELECT student_id, student.sname Score from
left score.student_id the Join student ON = student.sid
the WHERE Score not in .course_id (
the SELECT cid from the Join teacher course, left the WHERE ON course.teacher_id = teacher.tid tname = 'Zhang Lei teacher'
)
Group by student_id
39, two or more inquiries failed the course the students learn numbers and their grade point average;
the student_id SELECT, COUNT (. 1) score from NUM WHERE <60 Group by the student_id HAVING COUNT (. 1)> 2
40, to retrieve "004" course fraction is less than 60, No. students learn by score in descending order;
Score the WHERE NUM student_id from the SELECT <= 60 and 4 COURSE_ID the Order by NUM desc;
41, deleting scores "002" students "001" course;
the Delete from the WHERE COURSE_ID Score = 1 = 2 and student_id