Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql query where x is a value between two columns

Tags:

sql

sql-server

Given a table with the following columns

comment
minAge
maxAge

and the following rows:

comment, minAge, maxAge
"Baby", 1, 5
"Teen", 13, 19
"Adult", 20, 50

Is it possible to do a sql query like:

select * from rows where 16 between minAge and maxAge

With the results:

"Teen", 13, 19
like image 732
Trevor Avatar asked Aug 05 '13 17:08

Trevor


People also ask

How do you check if a value is between two numbers in SQL?

The SQL BETWEEN condition allows you to easily test if an expression is within a range of values (inclusive). The values can be text, date, or numbers. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How do you write a query to compare two columns in SQL?

Here's the generic SQL query to two compare columns (column1, column2) in a table (table1). mysql> select * from table1 where column1 not in (select column2 from table1); In the above query, update table1, column1 and column2 as per your requirement.

Can we use two columns in WHERE clause in SQL?

Answer. Yes, within a WHERE clause you can compare the values of two columns.


1 Answers

BETWEEN operator selects values within a range. The values can be numbers, text, or dates.

See your table and data yourself here

like image 152
Praveen Prasannan Avatar answered Oct 25 '22 16:10

Praveen Prasannan