Statements Flashcards
(52 cards)
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;
Structure of selecting records and adding a column with static value with Alias.
Select ‘Customers’ as Type, Customers.CustomerName as Name FROM Customers
UNION
Select ‘Suppliers’ as Type, Suppliers.SupplierName as Name FROM Suppliers;
Structure of Group By query
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s);
Countries wise count of customers with countries having more customers first
SELECT COUNT(CustomerId), Country FROM Customers GROUP BY Country ORDER BY Count(CustomerId) DESC;
List number of orders shipped by each shipper in descending sorted order of no of orders
SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;