Statements Flashcards
Difference between “Union” and “Union All”?
Difference is that Union does Distinct clause in the background while Union All does not perform distinct and gives all records merged.
Structure of basic Union Query
SELECT Column From Table1
UNION
SELECT Column From Table2;
Structure of basic Union All Query
Select Column From Table1
UNION ALL
Select Column From Table2;
What conditions must be satisfied for Union query?
- No of columns must match
- Order of columns should be same
- Data types should match
Types of Joins
- Inner
- Left Outer
- Right Outer
- Full
Define Inner Join
Intersection of two joining tables. So only the records that strictly satisfy the condition will be shown.
Define Left Outer Join
All records in left table and the ones that satisfy the condition in right table. The rows of right for which the condition is satisfied will be merged with left. For the records in left which don’t satisfy condition the columns of right table will have NULL value
Define Right Outer Join
All records in right table and the ones that satisfy the condition in left table. The rows of left for which the condition is satisfied will be merged with right. For the records in right which don’t satisfy condition the columns of left table will have NULL value
Define Full Join
All records of both tables are shown. Those for which condition matches are merged together. POTENTIALLY HUGE RECORDS ARE RETURNED FROM FULL JOINS.
Structure of Inner Join
SELECT Table1.Column1, Table2.Column1 FROM Table1 INNER JOIN Table2 ON Table1.MagicalCol1 = Table2.MagicalCol1
Structure of Left Join
SELECT Table1.Column1, Table2.Column1 FROM Table1 LEFT JOIN Table2 ON Table1.MagicalCol1 = Table2.MagicalCol1
Structure of Right Join
SELECT Table1.Column1, Table2.Column1 FROM Table1 RIGHT JOIN Table2 ON Table1.MagicalCol1 = Table2.MagicalCol1
Structure of Full Join
SELECT Table1.Column1, Table2.Column1 FROM Table1 FULL JOIN Table2 ON Table1.MagicalCol1 = Table2.MagicalCol1
Joining three tables
SELECT Table1.Column1, Table2.Column2, Table3.Column3 From ((Table1 INNER JOIN Table2 ON Table1.MagicColumn = Table2.MagicColumn) INNER JOIN Table3 ON Table1.MagicColumn = Table3.MagicColumn);
What is alias?
Alias as name suggests provides temporary name to columns or tables.
Two types
- Column alias
- Table alias
What is table alias?
Table alias is given to make query shorter when dealing with multiple tables.
What is column alias and when to use it?
Giving temporary name to column for better readability.
Structure of table alias
SELECT A.Column, A.Column2 from Table1 as A;
SELECT A.Column, B.Column from Table1 A, Table2 B WHERE A.Column = B.Column;
Structure of Column Alias
SELECT Column1 as ReadableCol, Column2 as [Readable Col] FROM Table1;
Structure of concatenating multiple column values in one
SELECT Column1, Column2 + ‘, ‘ + Column3 + ‘, ‘ + Column4 AS Alias FROM Table1;