Professional Documents
Culture Documents
Mysql Ppt1
Mysql Ppt1
Mysql Ppt1
Q L
B A S E
M yS
TA
DA D B M S
M N
O W CO LU
R
R Y N A L
M A A T I O
P R I
Y R E L BA S E
K E D AT A
Review of Chapter 1
MySQL Querying And
SQL Functions
DDL DML
1. DDL stands for Data DML stands for Data
Definition Language Manipulation Language
2.DDL is used to create the DML is used to populate and
database schema manipulate the data in the table
3.The DDL language is used to The DDL language is used to
change the structure of the manage the data in the database.
database.
Sorting records
Order by clause is used with SELECT statement in the query
to sort the data in ascending or descending order of one or
more columns.
This is important to note that the data in table is not sorted, only the results that appear on the
screen are sorted.
Syntax:
SELECT <column name> [,<column name>, ….] FROM <table name>
[WHERE <condition>]
[ORDER BY <column name> [, <column name>…]];
Example:
(Sorting on single column)
SELECT * FROM EMPL ORDER BY ENAME;
Example :
(Sorting on Multiple columns)
SELECT * FROM EMPL ORDER BY ENAME, JOB;
BETWEEN OPERATOR
EXAMPLE:
UPDATE STUDENT
SET GUID = 101
WHERE RollNumber = 3;
Data Deletion
The DELETE statement is used to delete one or more
record(s) from a table.
Syntax:
DELETE FROM table_name
WHERE condition;
EXAMPLE:
DELETE FROM STUDENT WHERE RollNumber = 2;
Note:
Be careful to include WHERE clause while using DELETE statement to
delete records in a table. Otherwise, all the records in the table will
get deleted.
The DROP TABLE statement is used to remove the entire table from the
database along with the records
Syntax:
DROP TABLE table_name;
EXAMPLE:
DROP TABLE STUDENT;
Inserting NULL
mysql>INSERT INTO Student(Rollno, Name,
Gender, Marks, DOB)
VALUES(12,'Swati Mehra', 'F', NULL, NULL);
Eliminating Duplicate/Redundant Data
DISTINCT clause
➢ DISTINCT clause is used to remove
duplicate rows from the results of a
SELECT statement. It is used to retrieve
only unique values for a column in the
table.
➢ The DISTINCT keyword can be used
only once with a given SELECT statement.
Syntax:
SELECT DISTINCT <column-name> from
<table-name>;
Handling NULL Values
NULL represents a missing or unknown value.
For example, the village column in a table called address
will have no value for cities.
Hence, NULL is used to represent such unknown values. It is
important to note that NULL is different from 0 (zero).
Also, any arithmetic operation performed with NULL value
gives NULL.
For example, 100 + NULL = NULL because
NULL is undefined and unknown and,
hence, the result is also undefined.
In order to check for NULL value in a column, we use IS NULL
operator.
Handling NULL Values
Display details of all those students who have
not been awarded any marks.
What is a function ?
A set of predefined codes to do a specific task and return a
zero or more values.A function is used to perform some
particular task and it returns zero or more values as a
result.
What are the two categories of functions you have already
learnt?
➢ Single Row
➢ Multiple Row functions
What are single row functions?
➢ Single row functions works on a single row to return a
single value
Functions in
MySQL
ROUND( ) function
Functions in Single row functions: Numeric
MySQL
1. Let us now add a new column Commission to the SALE table. The
column Commission should have a total length of 7 in which 2 decimal
places to be there.
2. Let us now calculate commission for sales agents as 12 per cent of the
SalePrice, insert the values to the newly added column Commission
3.Display records of the table SALE where commission > 73000.
4. Display the salesprice and commission rounded to 1 decimal place
Functions in Single row functions: Numeric
MySQL
LIBRARY
Write the output for the query given:
1. SELECT book_name,
INSTR(book_name,'an') FROM
LIBRARY WHERE
INSTR(book_name,'an')>0;
Functions in Single row functions: Numeric
MySQL
CUSTOMER
Answers :
1. SELECT LOWER(CustName), UPPER(Email) FROM CUSTOMER;
Do as directed :
1. Take the input as todays date and display in the format :
Wednesday,9 November 2022
Functions in Single row functions: String Functions
MySQL
EMP
CODE NAME SAL
E1 Ram Kumar 4000
E2 Suchitra 4500
E3 Yogendra 3000
E4 Sushil 3500
E5 Rajesh 4000
EMP
CODE NAME SAL
E1 Ram Kumar NULL
E2 Suchitra 4500
E3 Yogendra NULL
E4 Sushil 3500
E5 Rajesh 4000
mysql> SELECT COUNT(*) FROM EMP; 5
STUDENT
ROLL_NO NAME STREAM
101 Ankit Sharma Science
102 Payal Goel Commerce COUNT(DISTINCT(STREAM))
103 Gurpreet Kaur Humanities OUTPUT
3
104 Radhika Science
105 Akhay Dureja Commerce
Gender
Lab Activity:
GROUP BY
GROUP BY clause is used in SQL to group the rows on the basis
of common values in a column
Syntax:
SELECT <COLUMN NAME>,<AGGREGATE FUNCTION>
FROM <TABLE NAME>
GROUP BY <COLUMN NAME>
Example :
HAVING Clause
HAVING Clause is used to specify conditions on the rows with
GROUP BY clause.
Syntax:
SELECT <COLUMN LIST> FROM <TABLE NAME>
GROUP BY < COLUMN NAME> HAVING CONDITION;
GROUP BY Clause
SALE
2. Display the number of people in each category of payment mode from the
table SALE.
SELECT PaymentMode, COUNT(PaymentMode) FROM SALE GROUP BY
Paymentmode ORDER BY Paymentmode;
HAVING Clause
SALE
EMP
CODE NAME SAL DEPT
E1 Ram Kumar NULL Sales
E2 Suchitra 4500 Accounts
E3 Yogendra 2000 Sales
E4 Sushil 3500 Accounts
E5 Rajesh 7000 IT
E6 Ranbir 5000 Operations
E7 Suhas 4000 IT
Write queries :
➢ To display the minimum and maximum salary of the employees
department wise
➢ To display the total salary of employees in each department
➢ To display the number of employees in each department
Worksheet:
Write a query to display the names of the shoes in the
ascending order of price
Practical applications:
The need for the following reports:
1. The management of the school may want to know
what is the total salary of teachers of various
departments. i.e., what is the total salary of
teachers in each department
2. The management may also want to know what is
the maximum, minimum salary of teachers in
each of these departments.
3. It may also be required to find the total number
of teachers in each department.
AGGREGATE FUNCTIONS AND GROUP BY CLAUSE
Consider the table HOSPITAL:
Output :
Using WHERE and
HAVING with GROUP BY
Now the management of the shoe factory may want to
know what is the total quantity of shoes, of sizes other
than 6, of various types. i.e., what is the total quantity of
shoes (of sizes other than 6) of type School, Office, and
Sports each whose total quantity greater than 1500.
Query :
SELECT type, SUM(qty) FROM shoes
where size<>6 Condition on individual rows /
GROUP BY type Checks individual rows
WHERE Vs HAVING:
WHERE is used to put a condition on individual row of
a table whereas HAVING is used to put condition on
individual group formed by GROUP BY clause in a
SELECT statement.