Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL Subquery Exercises: Fetch even numbered records from employees table

MySQL Subquery: Exercise-15 with Solution

Write a query to fetch even numbered records from employees table.

Sample table: employees


Code:

SET @i = 0; 
SELECT i, employee_id 
FROM (SELECT @i := @i + 1 AS i, employee_id FROM employees)
a WHERE MOD(a.i, 2) = 0;

The code have been executed in MySQL Version : 5.4.

Here is the output

---+--------------
i  |  employee_id
---+--------------
2  |  101
4  |  103
6  |  105
8  |  107
10 |  109
12 |  111
14 |  113
16 |  115
18 |  117
20 |  119
22 |  121
24 |  123
........

 

Structure of 'hr' database:

hr database

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a query to display the employee ID, first name, last name, salary of all employees whose salary is above average for their departments.
Next:Write a query to find the 5th maximum salary in the employees table.

What is the difficulty level of this exercise?