SQL Interview Preparation
SQL Interview Preparation
1. What is the difference between INNER JOIN and OUTER JOIN
Assuming you're joining on columns with no duplicates, which is a very common case:
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a Venn diagram intersection.
An outer join of A and B gives the results of A union B, i.e. the outer parts of a Venn diagram union.
Eg.
Suppose you have two tables, with a single column each, and data as follows:
A B
- -
1 3
2 4
3 5
4 6
Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.
Inner join
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
select * from a INNER JOIN b on a.a = b.b;
select a.*, b.* from a,b where a.a = b.b;
a | b
--+--
3 | 3
4 | 4
Left outer join
A left outer join will give all rows in A, plus any common rows in B.
select * from a LEFT OUTER JOIN b on a.a = b.b;
select a.*, b.* from a,b where a.a = b.b(+);
a | b
--+-----
1 | null
2 | null
3 | 3
4 | 4
Right outer join
A right outer join will give all rows in B, plus any common rows in A.
select * from a RIGHT OUTER JOIN b on a.a = b.b;
select a.*, b.* from a,b where a.a(+) = b.b;
a | b
-----+----
3 | 3
4 | 4
null | 5
null | 6
Full outer join
A full outer join will give you the union of A and B, i.e. all the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.
select * from a FULL OUTER JOIN b on a.a = b.b;
a | b
-----+-----
1 | null
2 | null
3 | 3
4 | 4
null | 6
null | 5 ssuming you're joining on columns with no duplicates, which is a very common case:2. What is the difference between UNION and OUTERJOIN
UNION puts lines from queries after each other, while JOIN makes a cartesian product and subsets it -- completely different operations. Trivial example of UNION:mysql> SELECT 23 AS foo
-> UNION
-> SELECT 45 AS foo;
+-----+
| bah |
+-----+
| 23 |
| 45 |
+-----+
2 rows in set (0.00 sec)
similarly trivial example of JOIN:
mysql> SELECT * FROM
-> (SELECT 23 AS bah) AS foo
-> JOIN
-> (SELECT 45 AS bah) AS bar
-> ON (33=33);
+-----+-----+
| foo | bar |
+-----+-----+
| 23 | 45 |
+-----+-----+
1 row in set (0.01 sec)3. What is the difference between UNION and UNION ALL
* The UNION command combines the result set of two or more SELECT statements (only distinct values)
* Union removes duplicate records
Eg
* The UNION ALL command combines the result set of two or more SELECT statements (allows duplicate values).
Eg.
What is the difference between Primary key and Unique key
Primary Key is a column that is used to uniquely identify each tuple of the table.
It is used to add integrity constraints to the table. Only one primary key is allowed to be used in a table. Duplicate and NULL (empty) values are not valid in the case of the primary key. Primary keys can be used as foreign keys for other tables too.
Let’s take an example,
We have a table name employee which stores data of employees of a company. The below table shows the contents of the table.
| Emp_id | Name | Ph_No. | Position | Salary |
Emp_id here is primary key of the table. As the id of each employee is unique and no two employees can have the same Emp_id.
Unique Key
Unique key is a constraint that is used to uniquely identify a tuple in a table.
Multiple unique keys can present in a table. NULL values are allowed in case of a unique key. These can also be used as foreign keys for another table.
Let’s take an example,
We have a table name employee which stores data of employees of a company. The below table shows the contents of the table.
| Emp_id | Name | Ph_No. | Position | Salary |
Ph_No here is the foriegn key of the table. As the phone number of each employee is unique and it might be possible that an employee does not have any phone number.
Difference between Primary Key and Unique Key
| Primary Key | Unique Key |
|---|---|
| Unique identifier for rows of a table | Unique identifier for rows of a table when primary key is not present |
| Cannot be NULL | Can be NULL |
| Only one primary key can be present in a table | Multiple Unique Keys can be present in a table |
| Selection using primary key creates clustered index | Selection using unique key creates non-clustered index |
Comments
Post a Comment