- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get all rows apart from first and last in MySQL
To get all rows apart from first and last, use subquery along with MIN() and MAX(). Let us first create a table −
mysql> create table DemoTable1917 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentCode int, StudentMarks int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1917(StudentCode,StudentMarks) values(78,95); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode,StudentMarks) values(78,96); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode,StudentMarks) values(78,97); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode,StudentMarks) values(78,98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1917(StudentCode,StudentMarks) values(78,99); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1917;
This will produce the following output −
+-----------+-------------+--------------+ | StudentId | StudentCode | StudentMarks | +-----------+-------------+--------------+ | 1 | 78 | 95 | | 2 | 78 | 96 | | 3 | 78 | 97 | | 4 | 78 | 98 | | 5 | 78 | 99 | +-----------+-------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to get all rows apart from first and last:
mysql> select * from DemoTable1917 where StudentId NOT IN ( ( select min(StudentId) from DemoTable1917 where StudentCode=78), (select max(StudentId) from DemoTable1917 where StudentCode=78) ) and StudentCode=78;
This will produce the following output −
+-----------+-------------+--------------+ | StudentId | StudentCode | StudentMarks | +-----------+-------------+--------------+ | 2 | 78 | 96 | | 3 | 78 | 97 | | 4 | 78 | 98 | +-----------+-------------+--------------+ 3 rows in set (0.00 sec)
- Related Articles
- Get first and last elements from Vector in Java
- Get first and last elements from Java LinkedList
- Get the first and last date of next month in MySQL?
- How to select all rows from a table except the last one in MySQL?
- How to get first and last elements from ArrayList in Java?
- Write a Python program to read an Excel data from file and read all rows of first and last columns
- How to select last 10 rows from MySQL?
- How to get the first and last record of the table in MySQL?
- Select all rows except from today in MySQL?
- Get the last days of all the months in MySQL?
- Java Program to Get First and Last Elements from an Array List
- Golang program to get the first and last element from a slice
- Get values from all rows and display it a single row separated by comma with MySQL
- Get the sum of a column in all MySQL rows?
- How to select first and last data row from a MySQL result?

Advertisements