Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

MySQL INTERVAL Operator

by suresh

The MySQL Interval Operator uses the binary search method to search the items and returns the values from 0 to N. In this article, we show how to use the MySQL Interval Operator with multiple examples.

MySQL Interval Operator Syntax

The basic syntax of the interval in MySQL is:

SELECT INTERVAL (N, N1, N2, N3,....Nn) 

If N < N1, then 0 is returned. N < N2 means 1, N < N3 means 2 etc. This Interval function requires the arguments in ascending order. I mean, N1 < N2 < N3 < N4…….< Nn. Otherwise, it won’t work.

MySQL Interval Example

In this example, we are using the same values from N1 to N7. However, we are changing the N values to check the interval position.

SELECT INTERVAL(45, 10, 20, 30, 40, 50, 60, 70);

SELECT INTERVAL(11, 10, 20, 30, 40, 50, 60, 70);

SELECT INTERVAL(67, 10, 20, 30, 40, 50, 60, 70);

OUTPUT

MySQL INTERVAL Operator 1

Within the first statement, we used the N value less than N1 so, it returned 0. Next, we used the N Value greater than N7 so, 7 has returned. Within the third statement, we used NULL as N. This is the reason, MySQL has returned -1.

SELECT INTERVAL(6, 10, 20, 30, 40, 50, 60, 70);

SELECT INTERVAL(98, 10, 20, 30, 40, 50, 60, 70);

SELECT INTERVAL(NULL, 10, 20, 30, 40, 50, 60, 70);

OUTPUT

MySQL INTERVAL Operator 2

MySQL Interval Operator Example 2

The following query finds the interval position of a Customer based on the value 76000. First, 76000 act as N and employee income as N1. Next, Income acts as N and 76000 as N1.

Here, we used Order By clause to sort the Income in ascending order; otherwise, the Interval function won’t work.

SELECT EmpID, 
`First Name`,
      `Last Name`,
      Qualification,
      Occupation,
      Income,
      INTERVAL(76000, Income) AS Res1,
      INTERVAL(Income, 76000) AS Res2,
      Sales
FROM `MySQL Tutorial`.customer
ORDER BY Income ASC;

OUTPUT

MySQL INTERVAL Operator 3

Placed Under: MySQL

  • How to Download MySQL
  • Install MySQL on Windows
  • MySQL Create Database
  • MySQL Delete Database
  • MySQL Create Table
  • MySQL Drop Table
  • MySQL SELECT Statement
  • MySQL ALIAS Column
  • MySQL Distinct
  • MySQL Insert Statement
  • MySQL Delete
  • MySQL Truncate Table
  • MySQL WHERE Clause
  • MySQL Order By
  • MySQL Group By
  • MySQL Having Clause
  • MySQL LIMIT
  • MySQL Arithmetic Operators
  • MySQL COALESCE Function
  • MySQL AND Operator
  • MySQL NOT Operator
  • MySQL OR Operator
  • MySQL XOR Operator
  • MySQL BETWEEN Operator
  • MySQL Not Between Operator
  • MySQL GREATEST Function
  • MYSQL LEAST Function
  • MySQL LIKE Operator
  • MySQL NOT LIKE Operator
  • MySQL IFNULL Operator
  • MySQL NULLIF Operator
  • MySQL INTERVAL Operator
  • MySQL IS Operator
  • MySQL IN Operator
  • MySQL NOT IN Operator
  • MySQL IS NOT NULL
  • MySQL IS NULL
  • MySQL Inner Join
  • MySQL Cross Join
  • MySQL Right Join
  • MySQL Left Join
  • MySQL Aggregate Functions
  • MySQL Date Functions
  • MySQL Date Function
  • MySQL String Functions
  • MySQL Numeric Functions
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy