我有以下查询来获取studentsubject表中的第二行
它运作良好
但是我需要另一种方法来提供比使用子查询更好的性能
select SbID , StID, max(mark)
from studentsubject
where mark <( select max(mark) from studentsubject group by SbID)
group by SbID
order by mark desc
limit 5
注意:我有5门课程,我需要每门课程的第二高分
--
-- File generated with SQLiteStudio v3.0.3 on الأربعاء نيسان 1 12:43:34 2015
--
-- Text encoding used: windows-1256
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;
-- Table: subject
CREATE TABLE subject (
SubjectID [INT IDENTITY] (1, 1) PRIMARY KEY,
SubjectName VARCHAR (50) NOT NULL
);
INSERT INTO subject (
SubjectID,
SubjectName
)
VALUES (
1,
'C#'
);
INSERT INTO subject (
SubjectID,
SubjectName
)
VALUES (
2,
'C++'
);
INSERT INTO subject (
SubjectID,
SubjectName
)
VALUES (
3,
'JavaScript'
);
INSERT INTO subject (
SubjectID,
SubjectName
)
VALUES (
4,
'DataBase'
);
INSERT INTO subject (
SubjectID,
SubjectName
)
VALUES (
5,
'Asp.net'
);
-- Table: student
CREATE TABLE student (
StudentID [INT IDENTITY] (1, 1) PRIMARY KEY,
StudentName VARCHAR (50) NOT NULL
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
1,
'Ahmad Ali'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
2,
'Khaled Ahmad'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
3,
'Mohammad Ahmad'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
4,
'Mohammad A.Sharif'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
5,
'Mahmoud Zakal'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
6,
'Morad Tamimi'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
7,
'Khalid Ahson'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
8,
'Eyad Buzzoum'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
9,
'Almeqdad a.ateeleh'
);
INSERT INTO student (
StudentID,
StudentName
)
VALUES (
10,
'Mohammad Abu Naser'
);
-- Table: studentsubject
CREATE TABLE studentsubject (
StID INT NOT NULL,
SbID INT NOT NULL,
mark DECIMAL (2, 2) NOT NULL,
FOREIGN KEY (
StID
)
REFERENCES student (StudentID),
FOREIGN KEY (
SbID
)
REFERENCES subject (SubjectID),
PRIMARY KEY (
StID,
SbID
)
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
1,
1,
90.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
1,
2,
97.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
1,
3,
87.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
1,
4,
87
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
1,
5,
83
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
2,
1,
88
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
2,
2,
80
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
2,
3,
83
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
2,
4,
81
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
2,
5,
79
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
3,
5,
79
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
3,
1,
82
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
3,
2,
72
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
3,
3,
84
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
3,
4,
87
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
4,
5,
85
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
4,
1,
83
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
4,
2,
84
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
4,
3,
88
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
4,
4,
87
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
5,
1,
95.1
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
5,
2,
92.6
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
5,
3,
90.2
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
5,
4,
82.3
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
5,
5,
89.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
6,
1,
85.1
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
6,
2,
82.6
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
6,
3,
92.2
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
6,
4,
96.3
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
6,
5,
85.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
7,
1,
85.1
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
7,
2,
87.6
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
7,
3,
75.2
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
7,
4,
92.3
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
7,
5,
84.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
8,
1,
79.1
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
8,
2,
77.6
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
8,
3,
75.2
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
8,
4,
72.3
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
8,
5,
84.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
9,
1,
88.1
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
9,
2,
86.6
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
9,
3,
84.2
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
9,
4,
92.3
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
9,
5,
83.5
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
10,
1,
83.6
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
10,
2,
82.7
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
10,
3,
85.2
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
10,
4,
88.7
);
INSERT INTO studentsubject (
StID,
SbID,
mark
)
VALUES (
10,
5,
80.2
);
COMMIT TRANSACTION;
答案 0 :(得分:2)
你可以OFFSET
来自第二行的请求并限制4
上的请求,以便获取5 rows
select SbID , StID, max(mark)
from studentsubject
group by SbID
order by mark desc
limit 4 OFFSET 1