0% found this document useful (0 votes)
8 views5 pages

2023.08.15-Nullif, Diff BTWN Coalesce&nullif, Joins Basics-Notes

The document explains SQL functions such as COALESCE and NULLIF for handling null values, and discusses the use of CASE statements and UNION for applying multiple conditions. It outlines the differences between set operators and joins for merging tables, including various types of joins like inner, outer, and cross joins. Additionally, it provides examples of SQL queries to illustrate these concepts.

Uploaded by

Shantanu Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

2023.08.15-Nullif, Diff BTWN Coalesce&nullif, Joins Basics-Notes

The document explains SQL functions such as COALESCE and NULLIF for handling null values, and discusses the use of CASE statements and UNION for applying multiple conditions. It outlines the differences between set operators and joins for merging tables, including various types of joins like inner, outer, and cross joins. Additionally, it provides examples of SQL queries to illustrate these concepts.

Uploaded by

Shantanu Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Case --> whenever you want to apply miltple condition with multple values then we

can use case statement


Coalesce --> to replace null values with not null values we can use coalesce
syntax: coalesce(col1,col2,col3...)

Hot it will work: whatever first column is having not null value that column data
will display in output

*****************************************************************************

Nullif():

As per the requirement if you want to convert not null values to null values then
we have to use nullif() function.

syntax:
nulif(col1,value1)

eg: nullif(did,10)

notnull --> null [10 --> null] ==> nullif()

null --> notnull [null --> 10] ==> coalesce()

Both are completely different

*****************************************************************************
Difference between coalesce() and nullif():
coalesce() --> null to not null
nulif() --> not null to null

case or union ==>


*****************************************************************************
did - 10 and 20 --> null ==> nullif it is not possible

case when did in (10,20) then null else did

select eid,did from employee where did not in(10,20)


UNION
select eid,null as did from employee where did in(10,20)

q1
union
q2

q2
union
q1
==> Both are same
q1
minus
q2

q2
minus
q1
==> Both are not same
*****************************************************************************

Set operators: To perform merging operation between multple table in vertical


direcrion then we have to use SET operators
*****************************************************************************

JOINS: To perform merging operation between multple tables in Horizantol direcrion


then we have to use JOINS

Joins are complex --> Here we need to understand basics of joins, then it will
become very very easy

How we need to frame the query:

Select *
From Emp
JOIN DEP
ON emp.did=dep.did

Select e.eid,e.ename,e.did,d.dname
From Employee e
JOIN DEP d
ON e.did=d.did

e.did
d.did ==> it will give differnt kind of output while using outer joins

1. select * from emp


2. If you want to add secon table then we are using JOIN
3. While using join keyword we need to pass JOIN condition by using ON keyword
4. If same column present in multple tables then we have to use table aliasing
5. As per best practices in real time projects in select statement better to give
table alias for each column

tablename.columnname --> because to avoid abmguious error

as per the requirement we need to give join condition with proper column names

Normal condition --> Where (did=20)


JOIN condition --> ON

***********************************************************************
JOIN 10 table or 20 tables ==> Yes

Select e.eid,e.ename,d.did,d.dname,x.id1,y.id2
From Employee e
JOIN DEP d
ON e.did=d.did
JOIN X x
ON e.id=x.id
JOIN Y y
on e.id=y.id
***********************************************************************
Inner Joins ==> To get all matched records between both the tables
Outer Joins ==> to get matched and unamtched records
Left outer join
right outer join
Full outer join
Cross Join ==> to get combination of all the records

Select e.eid,e.ename,e.did,d.dname
From Employee e
Full outer JOIN DEP d
ON e.did=d.did
***********************************************************************

SQL PROMPT Examples:

mysql> select * from employee;


+------+-------+------+---------+
| eid | ename | did | country |
+------+-------+------+---------+
| 1 | x | 10 | IN |
| 2 | b | 20 | UK |
| 3 | b | 20 | UK |
| 4 | a | 30 | NULL |
| 5 | y | 30 | NULL |
| 6 | null | 40 | IN |
+------+-------+------+---------+
6 rows in set (1.48 sec)

mysql> select eid,did,nullif(did,20) as did1 from employee;


+------+------+------+
| eid | did | did1 |
+------+------+------+
| 1 | 10 | 10 |
| 2 | 20 | NULL |
| 3 | 20 | NULL |
| 4 | 30 | 30 |
| 5 | 30 | 30 |
| 6 | 40 | 40 |
+------+------+------+
6 rows in set (0.15 sec)

mysql> select eid,country from employee;


+------+---------+
| eid | country |
+------+---------+
| 1 | IN |
| 2 | UK |
| 3 | UK |
| 4 | NULL |
| 5 | NULL |
| 6 | IN |
+------+---------+
6 rows in set (0.00 sec)

mysql> select eid,country,coalesce(country,'US') as country1 from employee;


+------+---------+----------+
| eid | country | country1 |
+------+---------+----------+
| 1 | IN | IN |
| 2 | UK | UK |
| 3 | UK | UK |
| 4 | NULL | US |
| 5 | NULL | US |
| 6 | IN | IN |
+------+---------+----------+
6 rows in set (0.07 sec)

mysql> select * from employee;


+------+-------+------+---------+
| eid | ename | did | country |
+------+-------+------+---------+
| 1 | x | 10 | IN |
| 2 | b | 20 | UK |
| 3 | b | 20 | UK |
| 4 | a | 30 | NULL |
| 5 | y | 30 | NULL |
| 6 | null | 40 | IN |
+------+-------+------+---------+
6 rows in set (0.00 sec)

mysql> select * from employee;


+------+-------+------+---------+
| eid | ename | did | country |
+------+-------+------+---------+
| 1 | x | 10 | IN |
| 2 | b | 20 | UK |
| 3 | b | 20 | UK |
| 4 | a | 30 | NULL |
| 5 | y | 30 | NULL |
| 6 | null | 40 | IN |
+------+-------+------+---------+
6 rows in set (0.00 sec)

mysql> create table dep(did integer, dname varchar(10));


Query OK, 0 rows affected (1.86 sec)

mysql> insert into dep(10,'AA');


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'10,'AA')' at line 1
mysql> insert into dep values(10,'AA');
Query OK, 1 row affected (0.11 sec)

mysql> insert into dep values(20,'BB');


Query OK, 1 row affected (0.06 sec)

mysql> insert into dep values(30,'CC');


Query OK, 1 row affected (0.08 sec)

mysql> select * from employee;


+------+-------+------+---------+
| eid | ename | did | country |
+------+-------+------+---------+
| 1 | x | 10 | IN |
| 2 | b | 20 | UK |
| 3 | b | 20 | UK |
| 4 | a | 30 | NULL |
| 5 | y | 30 | NULL |
| 6 | null | 40 | IN |
+------+-------+------+---------+
6 rows in set (0.00 sec)

mysql> select * from dep;


+------+-------+
| did | dname |
+------+-------+
| 10 | AA |
| 20 | BB |
| 30 | CC |
+------+-------+
3 rows in set (0.00 sec)

mysql> select * from employee


-> join dep
-> on did=did;
ERROR 1052 (23000): Column 'did' in on clause is ambiguous
mysql> select * from employee
-> join dep
-> on employee.did=dep.did;
+------+-------+------+---------+------+-------+
| eid | ename | did | country | did | dname |
+------+-------+------+---------+------+-------+
| 1 | x | 10 | IN | 10 | AA |
| 2 | b | 20 | UK | 20 | BB |
| 3 | b | 20 | UK | 20 | BB |
| 4 | a | 30 | NULL | 30 | CC |
| 5 | y | 30 | NULL | 30 | CC |
+------+-------+------+---------+------+-------+
5 rows in set (0.13 sec)

mysql> Select eid,ename,did,dname


-> From Employee e
-> JOIN DEP d
-> ON e.did=d.did;
ERROR 1052 (23000): Column 'did' in field list is ambiguous
mysql> Select eid,ename,d.did,dname
-> From Employee e
-> JOIN DEP d
-> ON e.did=d.did;
+------+-------+------+-------+
| eid | ename | did | dname |
+------+-------+------+-------+
| 1 | x | 10 | AA |
| 2 | b | 20 | BB |
| 3 | b | 20 | BB |
| 4 | a | 30 | CC |
| 5 | y | 30 | CC |
+------+-------+------+-------+
5 rows in set (0.00 sec)

mysql>

You might also like