简体   繁体   中英

SQL missing expression

Pretty new to SQL. Have to answer this question. I have 2 tables. "employees" and "departments"

The question is. "The employee name, head of the department (HOD), and salary for all employees whose salary is not in the range of $5,000–$ 10,000"

This is what i've tried.

select employee_name, hod, salary
from employees
where salary < 5000 and > 10000;

Says missing expression under '>' 10000;

Any ideas? Do I need to join departments?

You have a syntax error, the last line needs to be (column name specified each time):

where salary < 5000 and salary > 10000;

But then it won't make sense as there's no value meeting these criteria. You need the conditions to use or :

where salary < 5000 or salary > 10000

to exclude salaries between 5000 and 10000.

You can also use BETWEEN to achieve the same:

where salary not between 5000 and 10000

Each criteria in the WHERE clause needs to stand on its own, for your 2nd criteria it doesn't know what field should be > 10000 , you have to specify:

select employee_name, hod, salary
from employees
where salary < 5000 
  and salary > 10000;

This will exclude all records since it's a per-row comparison and each record can only have one value of salary , so likely you want OR instead of AND , or maybe you have different fields in your actual query.

There is BETWEEN for defining a range of numbers/dates, but it is inclusive ie WHERE salary BETWEEN 5000 and 10000 would include everything >= 5000 and <= 10000, so not helpful in this situation, and some prefer to avoid it altogether as it can be tricky with dates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM