My SQL
My SQL
My SQL
AMUL SHRESTHA.
Lets Create a database.
..
•.
Assign data
Nname the table Define column
types of column
Syntax for creating table statement:
Create table employee(
e_id int not null,
e_name varchar(20),
e_salary int,
e_gender varchar(20),
e_dept varchar(20)
Primary key(e_id)
);
Inserting into employee table.
Insert into employee values(
1,’Jack’,100000,’Male’,’Account’
);
Selecting elements:
• SELECT column1,column2..
• From table name;
by avg(t_salary) DESC;
Having clause
• Having clause is used in combination with Group By to impose
conditions on groups. (Since where clause cant be used with
aggregate function)
• WHERE clause filters individual rows, whereas the HAVING clause filters groups
instead of one row at a time.
• We cannot use the WHERE clause with aggregate functions because it works for
filtering individual rows. In contrast, HAVING can works with aggregate functions
because it is used to filter groups.
• Row operations are handled by the WHERE clause, while the HAVING clause
handles column operations to summarized rows or groups.
• The WHERE clause retrieves the desired data based on the specified condition.
On the other hand, the HAVING clause first fetches whole data, and then
separation is done based on the specified condition.
SELECT COLUMN_NAME(S)
FROM TABLE_NAME
WHERE condition
Group BY column_name(s)
Having condition
Order by column_name(s);
• NOTE: HAVING CLAUSE MUST FOLLOW THE GROUP BY CLAUSE AND
MUST PRECEEDS THE ORDER BY CLAUSE.
• select t_subject,avg(t_salary) as avg_salary
• from teacher
• group by t_subject
• having avg(t_salary)>70000
Consider a table called 'mobilephones' with fields such as Id, Name,
Company, Quantity, and Price.
Write the query to select all mobile names whose average is greater
than 45000 from the 'Price' column in the 'mobilephones' table.
• SELECT Name, AVG(Price)
• FROM mobilephones
• GROUP BY Name
• HAVING AVG(Price) > 45000;
Write the query to select all mobile names whose sum is
smaller than 45000 from a 'Price' column in the
'mobilephones' table.
• SELECT Name, SUM(Price)
• FROM mobilephones
• GROUP BY Name HAVING SUM(Price) < 45000;
Delete
• Statement is used to delete existing records in the table.
• Implementation
• Select columns
• FROM table1
• RIGHT JOIN table2
• ON table1.column_x= table2.column_y;
FULL JOIN
• It returns all rows from left table and the right table with NULL values
in place where the join condition is not met.
implementation
• Select columns
• FROM table1
• full join table2
• ON table1.column_x= table2.column_y;