TASK–3
1. Grouping the rows, Where and Having Clauses , Diff b/w where and having
Grouping the rows . Group by function is used to group the rows based on a column we
specify.
Where – used to filter rows based on a condition , where keyword is used to filter before
grouping
Having – Filters groups based on aggregation . used usually only after groupby.
Ex :
Took an ecommerce table data.
code snippet:
select orderid, sum(totalamount) as amount
from ecommerce
group by OrderID
having sum(totalamount) > 1000;
output:
Difference between where & having keyword :
Where Having
It filters rows based on the given condition Filter after aggregation
Cannot has aggregate function Can work well with aggregate functions
Used before groupby Used after groupby
Aggregate functions :
SUM (), AVG(), MIN(), MAX(), COUNT()
TASK–3
2. What is a RELATION or TABLE .
Relation is a set of rows containing values that is grouped under the column.
Table stores the rows and columns and represents data for a specific entity like
students , employees , etc,.
3. Tuple and Attribute
Tuples is the ROWS in the table .
Attributes are the COLUMNS in the table
CREATE TABLE emp (
EMPLOYEE_ID INT,
EMPLOYEE_NAME VARCHAR(20),
EMPLOYEE_AGE INT
);
INSERT INTO emp VALUES ( 101 , 'PRAVEEN' , 24),(102 , 'AKHIL'
, 25) ;
SELECT * FROM emp;
Attributes -> employee_id , employee_name, employee_age.
Tuples -> the data in the rows 101, 102, etc …
4. Cartesian Product
Cartesian product is combining every row of one table with every row of another table.
so table 1 has x rows and table 2 has y rows, then it is x * y.
select * from students, emp;
5. Set theory concepts
Set → A table or relation.
Subset → A portion of the table.
TASK–3
Union → Combines rows from two sets (tables).
Intersection → Finds common rows between two sets.
6. KEYS
Primary Key Uniquely identifies each row in a table. No NULL should be .
Foreign Key Refers to a primary key in another table. used for relationships.
Candidate Key Any attribute (or group of attributes) that can uniquely identify a row.
Composite Key Combines multiple attributes to act as a primary key.
Unique Key Ensures all values are unique but allows one NULL (depends on DBMS).
7. JOINS
Inner join – Returns matching rows from both the tables .
Left join – Returns all rows from left table and matching rows from right table.
Right join – Returns all rows from right table and matching rows from left table.
Outer or Full outer join – Returns rows if there is a match in one or both tables
including all the data.
Cross join – x * y , cartesian product of two tables.