Wednesday, June 10, 2015

What will be the result of the query below?

SELECT CASE WHEN null = 'NULL' THEN 'Yup' else 'Nope' end as Result; ?

ans: nope

SELECT CASE WHEN null=null THEN 'Yup' else 'Nope' end as Result; ?

ans:nope

SELECT CASE WHEN null is null THEN 'Yup' else 'Nope' end as Result; ?

ans: yup

what is wrong below sqlquery?

SELECT id,year(billing_date) as billing_year
 FROM invoices WITH(NOLOCK)
WHERE billing_date>=2010

-----right query

SELECT id,year(billing_date) as billing_year
 FROM invoices WITH(NOLOCK)
WHERE year(billing_date) >=2010

-----check with example 


CREATE TABLE bbbb(id INT ,name CHAR(5))

    insert into bbbb 
     values (1,'babj'),(null,null),(null,'bb'),(4,null)

 SELECT CASE WHEN name is null  THEN 'yep' ELSE 'nope' END AS results FROM bbbb

    SELECT CASE WHEN name=null  THEN 'yep' ELSE 'nope' end as results FROM bbbb

    SELECT CASE WHEN name='null'  THEN 'yep' ELSE 'nope' END AS results FROM bbbb


     SELECT * FROM
bbbb (NOLOCK) WHERE id is null

     SELECT * FROM bbbb (NOLOCK) WHERE id is null or id<>1


link:   http://www.toptal.com/sql/interview-questions

No comments:

Post a Comment