in sql how the dbms execute the query - mysql

In nor_employees table, when I execute the below query,no rows are selected.
SELECT lastname, firstname, employee_id
FROM nor_employees
WHERE employee_id NOT IN
(SELECT reports_to FROM NOR_EMPLOYEES);
for the below query it is getting.
SELECT lastname, firstname, employee_id
FROM nor_employees
WHERE employee_id NOT IN
(SELECT reports_to FROM NOR_EMPLOYEES where reports_to is not null);
can you explain why?

There is a 3 value logic.
NOT IN clause is translated to not equal, i.e.
select *
from table
where col not in (10,20 )
it is translated by interpreter to
select *
from table
where col <> 10 and col <> 20
In case, if one of values in the list null then we have col <> Null. It always False. As result no rows in result set

Related

MySQL query delivers wrong result when using <> (not equal) in WHERE clause

I came accross a strange problem with a MySQL Query
SELECT COUNT(id) FROM members
100
SELECT COUNT(id) FROM members WHERE lastname = 'Smith'
20
SELECT COUNT(id) FROM members WHERE lastname <> 'Smith'
0
The problem is, that the last query (Members with lastname != 'Smith') returns 0.
If there are 100 members in total and 20 members named 'Smith', the number of member with other last names should be 80, shouldn't it?
I tried different version using <>, !=, enclosing Smith with ' or ". The result when using LIKE and NOT LIKE instead is the same.
How is this possible? It seems that I am missing something quite obvious, but what...?
because others are null
try this :
SELECT COUNT(id) FROM members WHERE IFNULL(lastname ,'--')<> 'Smith'
Example :
CREATE TABLE my_table
SELECT 'ersin' name FROM dual
union all
SELECT 'ersin' name FROM dual
union all
SELECT 'ersin' name FROM dual
union all
SELECT null name FROM dual
union all
SELECT null name FROM dual
union all
SELECT null name FROM dual;
select script:
select count(*) from my_table where IFNULL(name ,'--') <> 'ersin' ;
output:
count(*)
3

Replace null with value in mysql query with 0

I am trying replace null with 0 with following statement but returns no recrods instead of of catid supplied and 0.
select ifnull(count(*),0) as days, catid from mytable where Id=48 and catId=7
group by mytable.catId;
As far as I know, COUNT(*) does never return NULL. It returns 0 if there is no record.
count(*) never returns NULL, so you don't need any conditional logic:
select count(*) as days, catid
from mytable
where Id = 48 and catId = 7
group by mytable.catId;
Perhaps your issue is that the query is returning no rows. If so, you can leave out the group by. Then the query will always return one row:
select count(*) as days, catid
from mytable
where Id = 48 and catId = 7 ;

how does the select statement works or retrieve the data from database

here is the query
select emp1.EmployeeId as empss, EmployeeName, DepartmentId, IsActive, CreatedDate, Address, salary
FROM employee as emp1
where 4=(select count(emp.EmployeeId) as con from employee as emp where emp.EmployeeId < emp1.EmployeeId);
here is the table name employee where i am getting the number 5 row without limit,for writing this query i took some help over net but my question is how does the select statments work ??
1) does it select the column first then filter the where clause ?
2) does it filter the where clause then select column ??
please explain
thanks so much in advance
In MySQL Where conditions (filtration) executes before select columns. Following is the order of execution for various clauses present in a SELECT statement
FROM (including joins)
WHERE
SELECT (select columns)
GROUP BY
ORDER BY
LIMIT
Can validated by following SQL
SELECT 1 as cnt FROM employee WHERE cnt = 1;
This statement will throw error as the cnt is defined in the SELECT clause which executes after the WHERE but following SQL
SELECT 1 as cnt FROM employee GROUP BY cnt;
will work.
select * from employee;
this would return all rows from the table 'employee'
select id from employee;
this would return all rows but only the id column from the table 'employee'
select id, name from employee where id in (1,2);
this would return only matching rows for employee.id in (1,2) and the columns would be id and name
The 'from' will direct the db to goto that table and the conditions would filter out the needed rows - reducing scope further by traversing through and only printing the asked-for columns

SELECT WHERE IN - mySQL

let's say I have the following Table:
ID, Name
1, John
2, Jim
3, Steve
4, Tom
I run the following query
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill');
I want to get something like:
ID
1
2
NULL or 0
Is it possible?
How about this?
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill')
UNION
SELECT null;
Start by creating a subquery of names you're looking for, then left join the subquery to your table:
SELECT myTable.ID
FROM (
SELECT 'John' AS Name
UNION SELECT 'Jim'
UNION SELECT 'Bill'
) NameList
LEFT JOIN myTable ON NameList.Name = myTable.Name
This will return null for each name that isn't found. To return a zero instead, just start the query with SELECT COALESCE(myTable.ID, 0) instead of SELECT myTable.ID.
There's a SQL Fiddle here.
The question is a bit confusing. "IN" is a valid operator in SQL and it means a match with any of the values (see here ):
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill');
Is the same as:
SELECT Id FROM Table WHERE NAME = 'John' OR NAME = 'Jim' OR NAME = 'Bill';
In your answer you seem to want the replies for each of the values, in order. This is accomplished by joining the results with UNION ALL (only UNION eliminates duplicates and can change the order):
SELECT max(Id) FROM Table WHERE NAME = 'John' UNION ALL
SELECT max(Id) FROM Table WHERE NAME = 'Jim' UNION ALL
SELECT max(Id) FROM Table WHERE NAME = 'Bill';
The above will return 1 Id (the max) if there are matches and NULL if there are none (e.g. for Bill). Note that in general you can have more than one row matching some of the names in your list, I used "max" to select one, you may be better of in keeping the loop on the values outside the query or in using the (ID, Name) table in a join with other tables in your database, instead of making the list of ID and then using it.

SQl Query to retrieve data

I cannot figure out how to get the required data from my table. The query which I wrote shows an error saying subquery returns more than one row..
SELECT name
FROM `business`
WHERE id = (
SELECT business_id
FROM bill
WHERE id = (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )
Here the subquery for bill_schedule returns more than one row, where show_bill is a boolean column. All I want here is to display the 'name' from the business whose show_bill is set to 1.
SELECT `name`
FROM `business`
WHERE id in (
SELECT business_id
FROM bill
WHERE id in (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )
Since the sub query is returning multiple rows, you can't use an equality operator
Just change the = to IN in your where clause:
SELECT name
FROM `business`
WHERE id IN (
SELECT business_id
FROM bill
WHERE id IN (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )