Best way to fetch the Odd and Even Rows in SQL Table is done by using RowNumber, Show To display alternate records from MYSQL table, suppose we have the following records -
These are the methods that you can use to get alternate or ODD-EVEN records from a MySQL table - Method1 : MySQL MOD() methodMySQL MOD() method returns the remainder of a number divided by another number. So for getting alternate rows, we can divide the ID with 2 and displays only those having remainder 1.
Output of the above statement
The above statement returns only ODD rows. If you want to get even rows, write the statement as-
Output of the above statement Method 2We can also use the modulus operator instead of mod() method, like- the given statement returns only even rows-
Output of the above statement
similarly, the following statement returns only odd rows-
Output of the above statement
Related MySQL ExercisesMySQL concatenate How do I display all records in SQL?SELECT Syntax. SELECT column1, column2, ... FROM table_name;. SELECT * FROM table_name;. Example. SELECT CustomerName, City FROM Customers;. Example. SELECT * FROM Customers;. How do I get previous and next record in SQL?You can use UNION to get the previous and next record in MySQL. Insert some records in the table using insert command. Display all records from the table using select statement.
What are alternate queries in SQL?Alternative SQL queries are a family of query languages that allow developers to specify queries to SQL databases with languages other than the standard SQL. They are typically implemented for specific languages, such as for Scala, Scheme, Ruby and Haskell.
|