0% found this document useful (0 votes)
10 views

CC106 - MySQL

Sql
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CC106 - MySQL

Sql
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CC106 - Fundamentals of Database Systems

Midterm Exam Answers


1 2 3 4 5
To display details of all employees in descending order of their DOH.

SELECT * from employee ORDER BY doh DESC;

DB
MySQL ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.

The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
To display NAME, DESIGNATION, SALARY, HRA from tables EMPLOYEE and SALGRADE
where SALARY is less than 50000

SELECT e.name, e.designation, s.salary, s.hra FROM employee e


INNER JOIN salgrade s ON e.sg = s.salary_grade WHERE s.salary <50000;

DB
MySQL Aliases
Aliases are used to give a table, or a column in a table, a temporary name.

Aliases are often used to make column names more readable.

An alias only exists for the duration of that query.

An alias is created with the AS keyword.

SELECT column_name AS alias_name SELECT column_name(s)


FROM table_name; FROM table_name AS alias_name;
To display NAME AND DESIGNATION of those employees whose SG is either 'S02' or 'S03'

SELECT name, designation FROM employee WHERE sg='S02' or sg = 'S03' or


sg=’’ ;

SELECT name, designation FROM employee WHERE sg='S02' or 'S03';

DB
To display NAME AND DESIGNATION of those employees whose SG is either 'S02' or 'S03'

Another solution

SELECT name, designation FROM employee WHERE sg IN ('S02', 'S03');

DB
MySQL IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
DB
To display NAME AND DESIGNATION of those employees whose salary less than 25000

SELECT name, designation FROM employee WHERE sg IN (SELECT salary_grade


FROM salgrade WHERE salary<25000);

SELECT name, designation FROM employee WHERE sg IN (‘S03’);

DB
MySQL MIN() and MAX()
The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

SELECT MIN(column_name)
SELECT MAX(column_name)
FROM table_name
FROM table_name
WHERE condition;
WHERE condition;
Select sum(salary), avg(salary) from SALGRADE;

DB
To display details of all transactions of TYPE Withdraw from TRANSACT table

SELECT * from transact WHERE type=’Withdraw’;

DB
To display ANO, ANAME, AMOUNT and DOT of those persons from ACCOUNT and
TRANSACT table who have done transaction less than or equal to 3000
To display first date of transaction (DOT) from table TRANSACT for Account having ANO
as 102

SELECT MIN(dot) FROM transact WHERE ano = 102;

DB
To display first date of transaction (DOT) from table TRANSACT for Account having ANO
as 102

SELECT MIN(dot) AS 102_first_date_transaction FROM transact


WHERE ano = 102;

DB
MySQL Aliases
MySQL Aliases
SELECT COUNT(*), SUM(AMOUNT) FROM TRANSACT
WHERE DOT <= '2023-10-12';
MySQL COUNT(), AVG() and SUM() Functions
The COUNT() function returns the SELECT COUNT(column_name)
number of rows that matches a FROM table_name
WHERE condition;
specified criterion.
The AVG() function returns the SELECT AVG(column_name)
FROM table_name
average value of a numeric WHERE condition;
column.
SELECT SUM(column_name)
The SUM() function returns the FROM table_name
total sum of a numeric column. WHERE condition;
EXAMPLE
To display Name and Price of all the Accessories in ascending order of their Price

SELECT name, price FROM accessories ORDER BY price ;

DB
To display ID and SNAME of all the Shoppe location in 'Tangub'

SELECT id, sname FROM shoppe WHERE area='Tangub';

DB
SELECT id, sname FROM shoppe WHERE NOT area='Tangub';

DB
To display Name, Minimum and Maximum Price of each Name from ACCESSORIES table

SELECT name, MIN(price), MAX(price) FROM accessories GROUP BY name;

DB
MySQL MIN() and MAX()
The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

SELECT MIN(column_name)
SELECT MAX(column_name)
FROM table_name
FROM table_name
WHERE condition;
WHERE condition;
SELECT AVG(PRICE), MAX(PRICE) FROM ACCESSORIES WHERE PRICE>=10000;

DB

You might also like