Home » SQL

SQL Query to fetch (print) Even and Odd records from a table

Here, we will learn how to create a SQL Query that will fetch (print) Even and Odd numbers.
Submitted by Preeti Jain, on December 30, 2017

Given a table and we have to fetch (print) it’s Even and Odd record using SQL Query.

TABLE: EMPLOYEE

    ID	NAME	CONTACT	SALARY
    1	PREETI	123456	2000
    2	ARPIT	123789	3000
    3	ADI	147258	5000
    4	APOORV	789632	10000
    5	RIDDHI	148796	20000
    6	REX	148965	30000
    7	WENDY	128659	40000
    8	ANIKET	123489	50000

Case 1: Write a query to find even records in MYSQL.

mysql> select * from EMPLOYEE where id in(select id from EMPLOYEE where id%2 = 0);

Output/Result

ID	NAME	CONTACT	SALARY
2	ARPIT	123789	3000
4	APOORV	789632	10000
6	REX	148965	30000
8	ANIKET	123489	50000

Case 2: Write a query to find odd records in MYSQL.

mysql> select * from EMPLOYEE where id in(select id from EMPLOYEE where id%2 <> 0);

Output/Result

ID	NAME	CONTACT	SALARY
1	PREETI	123456	2000
3	ADI	147258	5000
5	RIDDHI	148796	20000
7	WENDY	128659	40000




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.