Friday 26 August 2016

SQL Interview Tricky SQL Questions

What does UNION do? What is the difference between UNION and UNION ALL ?
Answer:- UNION merges the contents of two structurally-compatible tables into a single combined table. The difference between  UNION and UNION ALL is that UNION will omit duplicate records whereas UNION ALL will include duplicate records.
It is important to note that the performance of UNION ALL will typically be better than UNION , since UNION  requires the server to do the additional work of removing any duplicates. So, in cases where is is certain that there will not be any duplicates, or where having duplicates is not a problem, use of UNION ALL would be recommended for performance reasons.

List and explain the different types of join  clauses supported in ANSI-standard SQL.
Answer:-ANSI-standard SQL specifies five types of JOIN clauses as follows:

INNER JOIN (a.k.a. “simple join”): Returns all rows for which there is at least one match in BOTH tables. This is the default type of join if no specific JOIN type is specified.
LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table, and the matched rows from the right table; i.e., the results will contain all records from the left table, even if the JOIN condition doesn’t find any matching records in the right table. This means that if the ON clause doesn’t match any records in the right table, the JOIN will still return a row in the result for that record in the left table, but with NULL in each column from the right table.
RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table, and the matched rows from the left table. This is the exact opposite of a LEFT JOIN; i.e., the results will contain all records from the right table, even if the JOIN condition doesn’t find any matching records in the left table. This means that if the ON clause doesn’t match any records in the left table, the JOIN will still return a row in the result for that record in the right table, but with NULL in each column from the left table.
FULL JOIN (or FULL OUTER JOIN): Returns all rows for which there is a match in EITHER of the tables. Conceptually, a FULL JOIN combines the effect of applying both a LEFT JOIN and a RIGHT JOIN; i.e., its result set is equivalent to performing a UNION of the results of left and right outer queries.
CROSS JOIN: Returns all records where each row from the first table is combined with each row from the second table (i.e., returns the Cartesian product of the sets of rows from the joined tables). Note that a CROSS JOIN can either be specified using the CROSS JOIN syntax (“explicit join notation”) or (b) listing the tables in the FROM clause separated by commas without using a WHERE clause to supply join criteria (“implicit join notation”).


Consider the following two query results:


SELECT count(*) AS total FROM orders;+-------+| total |+-------+|  100  |+-------+SELECT count(*) AS cust_123_total FROM orders WHERE customer_id = '123';+----------------+| cust_123_total |+----------------+|       15       |+----------------+Given the above query results, what will be the result of the query below?SELECT count(*) AS cust_not_123_total FROM orders WHERE customer_id <> '123'

Answer:-
The obvious answer is 85 (i.e, 100 - 15). However, that is not necessarily correct. Specifically, any records with a customer_id of NULL will not be included in either count (i.e., they won’t be included in cust_123_total, nor will they be included in cust_not_123_total). For example, if exactly one of the 100 customers has a NULL customer_id, the result of the last query will be:
+--------- ----------+
| cust_not_123_total |
+--------------------+
|         84         |
+--------------------+

What will be the result of the query below? Explain your answer and provide a version that behaves correctly.


select case when null = null then 'Yup' else 'Nope' end as Result;

ANSWER:-
This query will actually yield “Nope”, seeming to imply that null is not equal to itself! The reason for this is that the proper way to compare a value to null in SQL is with the is operator, not with =.
Accordingly, the correct version of the above query that yields the expected result (i.e., “Yup”) would be as follows:

select case when null is null then 'Yup' else 'Nope' end as Result;


Given two tables created and populated as follows:
INSERT INTO #t VALUES

CREATE TABLE #t(id int, user_id int);
CREATE TABLE #t1(idnum int, pageseq int, doctext varchar(100));

  (1,1),
  (2,2),
  (3,3);
INSERT INTO #t1(idnum,pageseq) VALUES
  (1,5),
  (2,6),
  (null,0);
What will the result be from the following query:
UPDATE #t1 SET doctext=pageseq FROM #t1 INNER JOIN #t ON #t.id=#t1.idnum
WHERE EXISTS (
  SELECT 1 FROM #t1
  WHERE id=#t.id
);

Explain your answer.