Introduction to fourteen cases of SQL database

Introduction to fourteen cases of SQL database

Data Sheet

/*
Navicat SQLite Data Transfer

Source Server : school
Source Server Version : 30808
Source Host : :0

Target Server Type : SQLite
Target Server Version: 30808
File Encoding: 65001

Date: 2021-12-23 16:06:04
*/

PRAGMA foreign_keys = OFF;

-- ----------------------------
-- Table structure for Course
-- ----------------------------
DROP TABLE IF EXISTS "main"."Course";
CREATE TABLE Course(
    courseid integer primary key autoincrement,
    courseme varchar(32),
    teacherid int
);

-- ----------------------------
-- Records of Course
-- ----------------------------
INSERT INTO "main"."Course" VALUES (3001, 'Chinese', 1001);
INSERT INTO "main"."Course" VALUES (3002, 'Mathematics', 1002);

-- ----------------------------
-- Table structure for Mark
-- ----------------------------
DROP TABLE IF EXISTS "main"."Mark";
CREATE TABLE Mark(
    userid integer,
    courseid integer not null,
    score int default 0
);

-- ----------------------------
-- Records of Mark
-- ----------------------------
INSERT INTO "main"."Mark" VALUES (2001, 3001, 89);
INSERT INTO "main"."Mark" VALUES (2001, 3002, 90);
INSERT INTO "main"."Mark" VALUES (2002, 3001, 66);
INSERT INTO "main"."Mark" VALUES (2003, 3002, 85);

-- ----------------------------
-- Table structure for sqlite_sequence
-- ----------------------------
DROP TABLE IF EXISTS "main"."sqlite_sequence";
CREATE TABLE sqlite_sequence(name,seq);

-- ----------------------------
-- Records of sqlite_sequence
-- ----------------------------
INSERT INTO "main"."sqlite_sequence" VALUES ('Teacher', 1002);
INSERT INTO "main"."sqlite_sequence" VALUES ('Student', 2002);
INSERT INTO "main"."sqlite_sequence" VALUES ('Course', 3002);

-- ----------------------------
-- Table structure for Student
-- ----------------------------
DROP TABLE IF EXISTS "main"."Student";
CREATE TABLE Student(
    userid integer primary key autoincrement,
    username varchar(32),
    userage int,
    usersex varchar(32)
);

-- ----------------------------
-- Records of Student
-- ----------------------------
INSERT INTO "main"."Student" VALUES (2001, 'Xiao Ming', 18, 'Male');
INSERT INTO "main"."Student" VALUES (2002, 'Xiaohong', 18, 'Female');

-- ----------------------------
-- Table structure for Teacher
-- ----------------------------
DROP TABLE IF EXISTS "main"."Teacher";
CREATE TABLE Teacher(
    teacherid integer primary key autoincrement,
    teachername varchar(32)
);

-- ----------------------------
-- Records of Teacher
-- ----------------------------
INSERT INTO "main"."Teacher" VALUES (1001, '张三');
INSERT INTO "main"."Teacher" VALUES (1002, 'Li Si');

question:

1. Query the student IDs of all students whose "Chinese" course scores are lower than those of "Mathematics" course

select a.userid from 
(select userid,score from Mark where courseid ='3001')a,
(select userid,score from Mark where courseid ='3002')b 
where a.userid = b.userid and a.score<b.score;

2. Query the student ID and average score of students whose average score is greater than 60 points

select userid,avg(score) from Mark
group by userid 
having avg(score)>60;

3. Query the student ID, name, number of courses selected, and total score of all students

select s.userid ,s.username ,count_courseid as number of selected courses, 
sum_score as Total score from Student s
left join 
(select userid,count(courseid ) as count_courseid,sum(score) as sum_score 
from Mark group by userid )sc
on s.userid = sc.userid;

4. Query the number of teachers with the last name 'Li':

select count(teachername )
from Teacher 
where teachername like '张%';

5. Search for the student IDs of students whose Chinese course scores are less than 60, sorted in descending order by scores:

select userid ,score
from Mark
where courseid = '3001'
and score<60
order by score desc;

6. Query the names of students who have studied or not studied any course taught by teacher "Zhang San"

select username  
from Student 
where userid in (
    select userid  
    from Mark,Course,Teacher 
    where Course.teacherid = Teacher.teacherid and Mark.courseid = Course.courseid
    and Teacher.teachername = 'Zhang San'
);

7. Query all students' elective courses, course numbers and course names:

select courseid ,courseme 
from Course 
where courseid in (select courseid from Mark group by courseid);

8. Retrieve the student ID of a student who has taken two courses:

select userid 
from Mark
group by userid 
having count(8) == 2;

9. Query each course and the corresponding number of elective students

select courseid ,count(*) from Course group by courseid ;

10. Query the name and grade of the student with the highest grade among the students who have chosen the course taught by teacher "Zhang San"

select Student.username ,Mark.score
from Mark 
left join Student on Mark.userid = Student.userid 
left join Course on Mark.courseid = Course.courseid 
left join Teacher on Course.teacherid = Teacher.teacherid 
where Teacher.teachername = 'Zhang San'
and Mark.score = (
select max(score) 
from Mark sc_1 
where Mark.courseid = sc_1.courseid);

11. The number of students who have chosen the course:

select count(2) from 
(select distinct userid from Mark)a;

12. Query the student ID and name of the student whose course code is "Chinese" and whose course score is above 80 points

select Mark.userid,Student.username  
from Mark 
left join Student on Mark.userid = Student.userid 
where Mark.courseid = '3001' and Mark.score>80;

13. Query the average score of each course, and sort the results in ascending order by average score. If the average scores are the same, sort them in descending order by course number.

select courseid ,avg(score)
from Mark 
group by courseid 
order by avg(score),courseid desc;

14. Query the names and scores of students whose course name is "Mathematics" and whose scores are higher than 85:

select c.courseme ,Student.userid ,Student.username ,Mark.score
from Course c
left join Mark on Mark.courseid = c.courseid 
LEFT JOIN Student on Student.userid = Mark.userid 
where c.courseme = 'Mathematics' and Mark.score>85;

This is the end of this article about the fourteen SQL database cases. For more relevant SQL database case content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Case analysis of several MySQL update operations
  • MYSQL's 10 classic optimization cases and scenarios
  • SQL Server batch insert data case detailed explanation

<<:  Detailed explanation of html printing related operations and implementation

>>:  A simple method to be compatible with IE6's min-width and min-height

Recommend

Detailed explanation of pipeline and valve in tomcat pipeline mode

Preface In a relatively complex large system, if ...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...

HTML uses canvas to implement bullet screen function

Introduction Recently, I needed to make a barrage...

Detailed explanation of the use of JavaScript functions

Table of contents 1. Declare a function 2. Callin...

Getting Started Tutorial on GDB in Linux

Preface gdb is a very useful debugging tool under...

Detailed explanation of the use of Linux time command

1. Command Introduction time is used to count the...

A brief discussion on the correct posture of Tomcat memory configuration

1. Background Although I have read many blogs or ...

Linux parted disk partition implementation steps analysis

Compared with fdisk, parted is less used and is m...

Canonical enables Linux desktop apps with Flutter (recommended)

Google's goal with Flutter has always been to...

How to change the MySQL database directory location under Linux (CentOS) system

How to change the MySQL database directory locati...

Introducing ECharts into the Vue project

Table of contents 1. Installation 2. Introduction...

How to change MySQL character set utf8 to utf8mb4

For MySQL 5.5, if the character set is not set, t...