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:
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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework