1,403 views
0 0 votes

consider the following table customer:

customer_id  cust_name     city        grade       salesman_id
-----------  ------------  ----------  ----------  -----------
3002         Nick Rimando  New York    100         5001
3005         Graham Zusi   California  200         5002
3001         Brad Guzan    London                  5005
3004         Fabian Johns  Paris       300         5006
3007         Brad Davis    New York    200         5001
3009         Geoff Camero  Berlin      100         5003
3008         Julian Green  London      300         5002
3003         Jozy Altidor  Moscow      200         5007

A SQL Query to display all the customers, who are either belongs to the city New York or not had a grade above 100.

option 1: 

SELECT * 
FROM customer 
WHERE city = 'New York' OR NOT grade>100;

option 2:

SELECT * 
FROM customer 
WHERE city = 'New York' OR grade<100;

my doubt is query 1 using a NOT operator which is the given answer, query 2 is my answer where I'm using < operator, so both queries will produce the same result for given problem???

1 Answer

Best answer
2 2 votes

Both queries will give different results.

A SQL Query to display all the customers, who are either belongs to the city New York or not had a grade above 100 i.e. grade is less than or equal to 100.

It will return the following 3 tuples

customer_id  cust_name     city        grade       salesman_id
-----------  ------------  ----------  ----------  -----------
3002         Nick Rimando  New York    100         5001
3007         Brad Davis    New York    200         5001
3009         Geoff Camero  Berlin      100         5003

Now, coming to your SQL queries, i think first is correct and second is incorrect. Also verified them online.

Second correct query should be :

whereas your second query results in :

which is incorrect!

• selected by
Position:
Show:

Related questions

1 1 vote
1 answers 1 answer
1.2k
1.2k views
tishhaagrawal asked Dec 16, 2023
1,212 views
My doubt here is, if NOT EXISTS gets an empty set as the input then every tuple of the table in the outer query must satisfy the condition. Am I right?For example, in the...
2 2 votes
1 1 answer
763
763 views
equimanthorn asked Sep 15, 2023
763 views
A SQL query is written in its format as clauses are arranged in a specific sequence and these clauses are executed in different sequence. If we’re writing a query using s...
0 0 votes
1 1 answer
1.8k
1.8k views
rayhanrjt asked Jan 6, 2023
1,816 views
Write SQL command to find DepartmentID, EmployeeName from Employee table whose average salary is above 20000.
2 2 votes
1 1 answer
860
860 views
Subhrangsu asked Jun 18, 2022
860 views
Write SQL query to show all employees hired on June 4,1984 (non-default format)emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)