My SQL

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

My-SQL

AMUL SHRESTHA.
Lets Create a database.
..

CREATE DATABASE DATABASENAME;

How do we USE [DATABASENAME];


create DB
Drop DATABASE DATABASENAME;
e_id e_name e_salary e_gender e_dept

1 Jack 100000 Male Account

2 Ryan 300000 Male Finance

3 alpha 500000 Female System

4 Kai 400000 Male Pharmacy

5 john 600000 male Analytics


• Not Null constraints.
• Primary key constraints. (Combination of not null+ Unique key
constraints.)
Creating table employee in sql.

•.

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;

• Select e_name from employee; // single element


• Select e_name,e_salary,e_dept from employee; //multiple element
Select distinct syntax
• SELECT DISTINCT column1,column2,…from table_name.
• Used to select only distinct values from our column

• select distinct e_name from employee;


Where clause
• SELECT COL1,COL2
FROM TABLE_NAME WHERE [CONDITION]

select * from employee where e_salary=100000;


Question select employee whose from account department.
Select employee whose salary is greater than 45000.
Using AND OR NOT OPERATORS
• AND operator displays records if all the conditions separated by
AND are TRUE. (multiple conditions.)
• SELECT column1,col2
From table_name where[condition1] AND [condition2] AND
[conditionN];
select * from employee where e_salary=100000 AND
e_Dept="Account";
OR OPERATOR
• Displays records if any of the conditions separated by OR is TRUE.
• SELECT column1,column2,column
From table_name WHERE [condition1]
OR[condition2]..or[conditionN];
select * from employee where e_dept="Analyst" or e_Dept="Account";
NOT OPERATORS
• NOT operator displays a record if the condition is NOT TRUE.
• Syntax:
• Select column1,column2,column
• FROM table_name WHERE NOT[CONDITION];

• Select * From employee where not e_dept=‘Account’;


LIKE OPERATOR
• LIKE OPERATOR IS USED TO ECTRACTE RECORDS WHERE A
PARTICULAR PATTERN IS PRESENT
• LIKE OPERATOR IS USED IN CONJUCTION WITH WILD CARD
CHARACTERS.
• There are 2 types of wild card characters
• %---- PERCENTAGE SYMBOL-- REPRESENT ZERO,ONE OR MULTIPLE
CHARACTERS
• _-> UNDERSCORE SYMBOL-> REPRESENTS MULTIPLE CHARACTERS.
LIKE OPERATOR SYNTAX
SELECT COL_LIST FROM TABLE_NAME WHERE
COLUMN_N LIKE ‘_XXX%’;
SELECT * FROM employee where e_name LIKE 'r%';

SELECT * FROM employee where e_dept LIKE 'A%‘;


SELECT * FROM employee where e_name LIKE 'r%';
SELECT * FROM employee where e_salary LIKE '1_____';
Between Operator
• Select col_list from tabe_name WHERE
• COLUMN_N BETWEEN VAL1 AND VAL2;

• Select * from employee where e_salary BETWEEN 100000 AND


230000
BASIC FUNCTIONS IN SQL
• MIN() FUNCTION
• MAX() FUNCTION
• COUNT() FUNCTION
• SUM() FUNCTION
• AVG() FUNCTION
MIN() FUNCTION gives you smallest
value
SELECT MIN(col_name)FROM
Table_name;
select min(e_salary) from employee;
MAX() function
• MAX() FUNCTION GIVES YOU LARGEST VALUE
• SELECT MAX(COL_NAME)FROM
• TABLENAME;

select max(e_salary) from employee;


Count () function
• Count () function returns the number of rows that match a specific
criteria.
SELECT COUNT(*) FROM TABLE_NAME
WHERE CONDITIONS
SELECT COUNT(*) FROM employee WHERE e_salary=100000;
Sum()
• SUM() function gives total sum of numeric column
Select sum(col_name) from
Table_name
Select sum(e_salary )from employee;
Avg()
• Avg() function gives the average value of a numeric column.
• Select Avg(col_name) from table name;
String Operators
• LTRIM()
• LOWER()
• UPPER()
• REVERSE()
• SUBSTRING()
Ltrim()
• select ltrim( ' ram');
Lower()
• Select lower(‘MY NAME IS RAM’);
UPPER()
• SELECT UPPER (‘my name is ram’);
REVERSE()
• Select reverse(‘I love nepal’);
substring
• select('this is ismt');
• select substring('this is ismt',9,4);
Order by and top clauses
• Order by are used to sort datas in ascending or descending postions.

Select column_list from table_name


Order by col1,col2 ,…ASC,DSC
• Select* from employee order by e_salary;
• Select* from employee order by e_salary DESC;
TOP CLAUSE
• SELECT TOP 3* FROM EMPLOYEE; unsupported in mysql
• select * from employee limit 3;
ORDER BY
select * from employee order by e_salary DESC limit 2;
update
• UPDATE teacher
• SET t_gender = 'female‘‚
• WHERE t_id = 3;
GROUP BY

• GROUP BY is used to get aggregate result with respect to group

• select avg(t_salary),t_gender from teacher GROUP BY t_gender;

• select avg(t_salary),t_gender from teacher group by t_gender order

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.

DELETE FROM table_name


[WHERE condition];
DELETE from employee WHERE e_age=40;
TRUNCATE
TRUNCATE TABLE table_name;

IT DELETES ALL THE DATA INSIDE THE TABLE


INNER JOIN
• Inner Join returns records that have matching values in both the
tables. It is also known as simple join.
Implementing the inner joins.
• Select columns
• FROM table1
• Inner join table2
• ON table1.column_x= table2.column_y;
Inner join
Left join
Syntax:
• Select columns
• FROM table1
• LEFT JOIN table2
• ON table1.column_x=table2.column_y;
Right join
• Right Join returns all the records from the right table, and the matched
records , from the left 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;

You might also like