You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Aggregate functions are allowed (SUM, AVG, MIN, MAX, COUNT)
Only supports filter conditions based on the existing columns
Support filters based on aggregated results
HAVING clause is used only after GROUP BY clause.
Aggregate Functions are use to summarize data (Returns single aggregated value for multiple rows)
SUM | AVG | COUNT | MAX | MIN
WHERE
SELECT*FROM employee
WHERE designation ='Data Scientist';
HAVING
SELECTMAX(salary)
FROM employee
GROUP BY designation
HAVINGMAX(salary) >100000;
3. UNION vs UNION ALL
Combines 2 or more tables
Tables must have same number of columns
Columns must have same data type
Columns must have same order of columns
UNION
UNION ALL
Keep distinct records
Keep duplicate records
UNION
SELECT*FROM sales
UNIONSELECT*FROM product
ORDER BY product_name;
UNION ALL
SELECT*FROM sales
UNION ALLSELECT*FROM product
ORDER BY product_name;
4. PRIMARY KEY vs UNIQUE
Primary Key
Unique Key
We can have only one PRIMARY KEY in a table
We can have more than one UNIQUE KEY in a table
Do not accept NULL values
Accepts only one NULL value.
Identify UNIQUE rows in the table
Maintain UNIQUE data in a column.
PRIMARY KEY
CREATETABLEperson
(
pID INTNOT NULLPRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT
);
UNIQUE KEY
CREATETABLEperson
(
pID INTNOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
age INT
);
5. PRIMARY KEY vs FOREIGN KEY
PRIMARY KEY
FOREIGN KEY
Identify UNIQUE row in the table
Column in a table which is PRIMARY KEY in another table
We can have only one PRIMARY KEY in the table
We can have more than one FOREIGN KEY in the table
PRIMARY KEY can not accept NULL value
FOREIGN KEY accepts NULL values
PRIMARY KEY will not allow Duplicate value
FOREIGN KEY will allow duplicate value.
PRIMARY KEY
CREATETABLEperson
(
pID INTNOT NULLPRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT
);
Unique Key
CREATETABLEperson
(
pID INTNOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
age INT
);
6. IN vs EXISTS
IN
Multiple OR
SELECT*FROM employee
WHERE city ='Mumbai'OR city ='Bangalore'OR city ='Pune';
SELECT*FROM employee
WHERE city IN (`Mumbai`, `Bangalore`, `Pune`);
SELECT*FROM sales
WHERE city IN (SELECT city FROM returns)
EXISTS
Returns either True or False value (Tests for existance of records in a sub query)
SELECT*FROM sales
WHERE EXISTS (SELECT city FROM returns
WHEREreturns.ID=sales.IDAND price <500)
7. ORDER BY vs GROUP BY
ORDER BY
GROUP BY
Sorting in ASC or DESC order
Used with Aggregate functions
ORDER BY
SELECT*FROM sales
ORDER BY city DESC;
GROUP BY
SELECT is_active, COUNT(*)
FROM customers
GROUP BY is_active;
WHERE Clause is used with SELECT Clause before GROUP BY
SELECT designation, salary
FROM employee
WHERE designation IN ('Data Scientist', 'Data Analyst', 'Business Analyst', 'Data Architect', 'Machine Learning Engineer')
GROUP BY designation;
HAVING Clause is used with GROUP BY and WHERE Clause can't be used after GROUP BY
SELECT model, price
FROM vehicles
GROUP BY model
HAVINGSUM(price) >5000000ORDER BY price DESC;
8. JOIN vs Sub Query
Combine data from different tables into a single table
Sub Query
-- Select only from first table:SELECT phone, customer_name
FROM customer
WHERE c_ID IN (SELECT c_ID
FROM orders)
JOIN
-- Select from either of the table:SELECT phone, customer_name, order_ID
FROM customers c
JOIN orders o
ONo.customer_ID=c.customer_ID
9. UNION vs JOIN
UNION
JOIN
Combine rows
Merge columns
Number of columns and data type of columns should be same
Combines on the basis of common column (ID/Values)
Vertical
Horizontal
UNION
SELECT city from table1
UNIONSELECT city from table2;
JOIN
SELECTa.city, b.nameFROM departments a
JOIN employee b
ONa.ID=b.ID;
10. Index
Index are used for fast retrieval of data.
Index in the database is very similar to an Index in a book.
e.g. If you want to refer to a pages in a book, you first refer to the Index.
Types of Indexes
Single Column Index: Index based on only one column.
CREATEINDEXindex_nameON table_name (column_name);
Unique Index: Does not allow any duplicate values to be inserted into the table.