ScanarioBasedSQL
ScanarioBasedSQL
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.
Surprisingly, given the sample data provided, the result of this query will be an empty
set. The reason for this is as follows: If the set being evaluated by the SQL NOT
IN condition contains any values that are null, then the outer query here will return an
empty set, even if there are many runner ids that match winner_ids in the races table.
Knowing this, a query that avoids this issue would be as follows:
Although there are 4 customers not referred by Jane Smith (including Jane Smith
herself), the query will only return one: Pat Richards. All the customers who were
referred by nobody at all (and therefore have NULL in their ReferredBy column) don’t
show up. But certainly those customers weren’t referred by Jane Smith, and certainly
NULL is not equal to 2, so why didn’t they show up?
SQL Server uses three-valued logic, which can be troublesome for programmers
accustomed to the more satisfying two-valued logic (TRUE or FALSE) most
programming languages use. In most languages, if you were presented with two
predicates: ReferredBy = 2 and ReferredBy <> 2, you would expect one of them to be
true and one of them to be false, given the same value of ReferredBy. In SQL Server,
however, if ReferredBy is NULL, neither of them are true and neither of them are false.
Anything compared to NULL evaluates to the third value in three-valued logic:
UNKNOWN.
…or:
COALESCE() )
Watch out for the following, though!
This will return the same faulty set as the original. Why? We already covered that:
Anything compared to NULL evaluates to the third value in the three-valued logic:
UNKNOWN. That “anything” includes NULL itself! That’s why SQL Server provides the
IS NULL and IS NOT NULL operators to specifically check for NULL. Those particular
operators will always evaluate to true or false.
Even if a candidate doesn’t have a great amount of experience with SQL Server, diving
into the intricacies of three-valued logic in general can give a good indication of whether
they have the ability learn it quickly or whether they will struggle with it.
The query will result in 50 rows as a “cartesian product” or “cross join”, which is the
default whenever the ‘where’ clause is omitted.
In SQL Server, PostgreSQL, and SQLite, this can be done using the except keyword
as follows:
select * from test_a
except
select * from test_b;
In Oracle, the minus keyword is used instead. Note that if there are multiple columns,
say ID and Name, the column should be explicitly stated in Oracle queries: Select ID
from test_a minus select ID from test_b
MySQL does not support the except function. However, there is a standard SQL
solution that works in all of the above engines, including MySQL:
select a.id
from test_a a
left join test_b b on a.id = b.id
where b.id is null ;
Given a table TBL with a field Nmbr that has rows with the
following values:
1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1
Write a query to add 2 where Nmbr is 0 and add 3
where Nmbr is 1.
Hide answer
update TBL set Nmbr = case when Nmbr = 0 then Nmbr+ 2 else Nmbr+ 3
end ;
First, the SELECT DISTINCT TOP (10) Salary FROM Employee ORDER BY Salary DESC query
will select the top 10 salaried employees in the table. However, those salaries will be
listed in descending order. That was necessary for the first query to work, but now
picking the top 1 from that list will give you the highest salary not the the 10th
highest salary.
Therefore, the second query reorders the 10 records in ascending order (which the
default sort order) and then selects the top record (which will now be the lowest of those
10 salaries).
Not all databases support the TOP keyword. For example, MySQL and PostreSQL use
the LIMIT keyword, as follows:
SELECT Salary FROM
(
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 10
) AS Emp ORDER BY Salary LIMIT 1;
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 9,1;
You can avoid duplicates using UNION ALL and still run much faster than UNION
DISTINCT (which is actually same as UNION) by running a query like this:
SELECT * FROM mytable WHERE a=X UNION ALL SELECT * FROM mytable WHERE b=Y AND
a!=X
The key is the AND a!=X part. This gives you the benefits of the UNION (a.k.a., UNION
DISTINCT ) command, while avoiding much of its performance hit.
user_id username
1 John Doe
2 Jane Don
3 Alice Jones
4 Lisa Romero
SELECT
u.user_id,
username,
training_id,
training_date,
count ( user_training_id ) AS count
FROM users u JOIN training_details t ON t.user_id = u.user_id
GROUP BY u.user_id,
username,
training_id,
training_date
HAVING count ( user_training_id ) > 1
ORDER BY training_date DESC ;
user_id username training_id training_date count
4 Lisa Romero 2 August, 04 2015 00:00:00 2
4 Lisa Romero 3 August, 03 2015 00:00:00 2
1 John Doe 1 August, 02 2015 00:00:00 3
3 Alice Jones 2 August, 02 2015 00:00:00 2
An execution plan is basically a road map that graphically or textually shows the data
retrieval methods chosen by the SQL server’s query optimizer for a stored procedure or
ad hoc query. Execution plans are very useful for helping a developer understand and
analyze the performance characteristics of a query or stored procedure, since the plan
is used to execute the query or stored procedure.
In many SQL systems, a textual execution plan can be obtained using a keyword such
as EXPLAIN , and visual representations can often be obtained as well. In Microsoft SQL
Server, the Query Analyzer has an option called “Show Execution Plan” (located on the
Query drop down menu). If this option is turned on, it will display query execution plans
in a separate window when a query is run.
SELECT TOP 100 user_id FROM dbo.users WHERE user_id % 2 = 1 ORDER BY user_id
How can you select all the even number records from a
table? All the odd number records?
Hide answer
To select all the even number records from a table:
Select * from table where id % 2 = 0
What are the NVL and the NVL2 functions in SQL? How do
they differ?
Hide answer
Both the NVL(exp1, exp2) and NVL2(exp1, exp2, exp3) functions check the
value exp1 to see if it is null.
With the NVL(exp1, exp2) function, if exp1 is not null, then the value of exp1 is
returned; otherwise, the value of exp2 is returned, but case to the same data type as
that of exp1 .
With the NVL2(exp1, exp2, exp3) function, if exp1 is not null, then exp2 is returned;
otherwise, the value of exp3 is returned.
What is the difference between
the RANK() and DENSE_RANK() functions? Provide an
example.
Hide answer
The only difference between the RANK() and DENSE_RANK() functions is in cases where
there is a “tie”; i.e., in cases where multiple values in a set have the same ranking. In
such cases, RANK() will assign non-consecutive “ranks” to the values in the set
(resulting in gaps between the integer ranking values when there is a tie),
whereas DENSE_RANK() will assign consecutive ranks to the values in the set (so there
will be no gaps between the integer ranking values in the case of a tie).
For example, consider the set {25, 25, 50, 75, 75, 100} . For such a set, RANK() will
return {1, 1, 3, 4, 4, 6} (note that the values 2 and 5 are skipped),
whereas DENSE_RANK() will return {1,1,2,3,3,4} .
In PostgreSQL one can also use this syntax to achieve the fully correct result:
ROLLBACK
SELECT * FROM Employees
Hide answer
1. Single-row functions work with single row at a time. Multiple-row functions work
with data of multiple rows at a time.
2. The group by clause combines all those records that have identical values in a
particular field or any group of fields.
When stored in a database, varchar2 uses only the allocated space. E.g. if you have
a varchar2(1999) and put 50 bytes in the table, it will use 52 bytes.
But when stored in a database, char always uses the maximum length and is blank-
padded. E.g. if you have char(1999) and put 50 bytes in the table, it will consume 2000
bytes.
Write an SQL query to display the text CAPONE as:
C
A
P
O
N
E
BEGIN
print( substring (@a,@i, 1 ));
set @i=@i+ 1 ;
END
In Oracle SQL, this can be done as follows:
ID
----
1
2
3
4
5
(5 rows)
5
10
15
Table is as follows:
I
D C1 C2 C3
1 0
0 1
0 1
0 1
1 0
0 1
1 0
1 0
Hide answer
update table set col2 = case when col1 = 1 then 0 else 1 end
In MySQL:
IN :
Declare @N int
set @N = 5 ;
WITH CTE AS
(
SELECT Name , Salary, EmpID, RN = ROW_NUMBER()
OVER ( ORDER BY Salary DESC )
FROM Employee
)
SELECT Name , Salary, EmpID
FROM CTE
WHERE RN = @N
SELECT col , case when upper ( col ) = lower ( col ) then 'Fizz' else
'Buzz' end as FizzBuzz from table ;
This will give the third-highest salary from the Employee table. Accordingly we can find
out Nth salary using LIMIT (N-1),1 .
But MS SQL Server doesn’t support that syntax, so in that case:
OFFSET 2 ROWS
FETCH NEXT 1 ROW ONLY
OFFSET ’s parameter corresponds to the (N-1) above.
Given the following table named A :
x
------
2
-2
4
-4
-3
0
2
select sum(case when x>0 then x else 0 end)sum_pos,sum(case when x<0 then x
else 0 end)sum_neg from a;
5.67
34.567
365.253
34
5.67 5 67
34.567 34 567
34 34 0
Hide answer
10 Anil 50000 18
11 Vikas 75000 16
12 Nisha 40000 18
13 Nidhi 60000 17
14 Priya 80000 18
15 Mohit 45000 18
16 Rajesh 90000 –
17 Raman 55000 16
18 Santosh 65000 17
16 Rajesh 65000
17 Raman 62500
18 Santosh 53750
Hide answer
3. FROM users
4. GROUP BY email
8. FROM users