Simple Queries in SQL
Simple Queries in SQL
MySQL
and
Simple Queries in SQL
1
18-10-2023
Advantages of MySQL
1) Reliability and performance
2) Easy to use
3) Free and supports SQL
4) Portable/multiplatform support
5) Datatypes
6) Integrity checks
7) Scalability and limits (Has powerful processing capabilities and can handle
large databases containing 60,000 tables and more then 50 million rows.
8) Security (by username and password authentication)
9) Connectivity (Client can connect to MySQL server using several protocols)
Starting MySQL
Workbench
Command line
2
18-10-2023
SQL Datatypes
Each value manipulated by SQL Database has a data type.
A data type specifies the possible values for a variable/identifier, the
operations that can be performed on that type and the way the values of
that type are stored.
The data type of a value associates a fixed set of properties with the value.
In SQL there are three main data types:
• Number
• Date
• Character/String
SQL operators
Arithmetic Operators
Relational Operators are:
Name Description
<, <=, >, >=, =, != (<>)
%, MOD Modulo operator
* Multiplication operator Logical operators are:
+ Addition operator AND (&&), OR (II), NOT(!)
- Minus operator
Unary minus, change the sign
- of the argument
/ Division operator
DIV Integer division
Imp:
• String or group of characters in single/double quotes
• Commands not case sensitive but data is case sensitive.
3
18-10-2023
• SELECT
• FROM
• WHERE
4
18-10-2023
Accessing Database
Command Description
show databases; Displays the list of all the databases.
5
18-10-2023
Ordering of columns
The column headings in the output table of a SQL command is in the same order as that of select
command, nothing changes in original table.
6
18-10-2023
ALL/DISTINCT Clause
Select [ALL | DISTINCT] <col list> from
table;
(default is ALL)
JOB
select distinct job
CLERK
from employee; SALESMAN
PRESIDENT
MANAGER
select job from employee; ANALYST
WHERE Clause
Output based on condition ENAME JOB
Select <col list> SMITH CLERK
JONES MANAGER
from table SCOTT ANALYST
where <cond> ADAMS CLERK
FORD ANALYST
from employee
where job = 'CLERK' OR job = 'ANALYST'; ENAME JOB
ALLEN SALESMAN
Or WARD SALESMAN
JONES MANAGER
select ename, job
MARTIN SALESMAN
from employee BLAKE MANAGER
CLARK MANAGER
where NOT (job = 'CLERK' || job = 'ANALYST');
KING PRESIDENT
TURNER SALESMAN
7
18-10-2023
BETWEEN Clause
BETWEEN clause
ENAME HIREDATE
Select ename, hiredate JONES 02-APR-81
from employee BLAKE 01-MAY-81
ENAME HIREDATE
Select ename, hiredate
SMITH 17-DEC-80
from employee ALLEN 20-FEB-81
IN Clause
Condition to select from a set/list – IN clause
select ename ENAME
from employee SMITH
JONES
where job IN ('CLERK', 'ANALYST', 'MANAGER');
BLAKE
CLARK
OR SCOTT
ADAMS
JAMES
select * from employee FORD
where job NOT IN ('CLERK', 'ANALYST', 'MANAGER'); MILLER
8
18-10-2023
Handling NULL
ifnull(field, 'text')
9
18-10-2023
Simple calculations
ENAME SAL*12
Displaying Annual salary of dept
20 SMITH 9600
JONES 35700
select ename, sal*12 SCOTT 36000
from employee ADAMS 13200
where deptno=20; FORD 36000
Simple calculations
Dual is a table with one row and one column without any data, can be
used for obtaining result of some calculations and for using some
functions.
10
18-10-2023
COLUMN alias
Select ename NAME, sal*12 "ANNUAL SALARY"
from employee;
OR
Select ename NAME, sal*12 AS "ANNUAL SALARY"
from employee; NAME ANNUAL SALARY
SMITH 9600
ALLEN 19200
WARD 15000
JONES 35700
MARTIN 15000
BLAKE 34200
CLARK 29400
SCOTT 36000
KING 60000
TURNER 18000
ADAMS 13200
JAMES 11400
FORD 36000
MILLER 15600
TEXT in a query
11
18-10-2023
PATTERN Matching
Pattern matching
% matches any substring (0 onwards)
_ matches exactly one character (no zero even)
12
18-10-2023
Sorting output
ENAME HIREDATE
Sorting Output –
ADAMS 23-MAY-87
Order by clause (ASC/DESC) SCOTT 19-APR-87
Select ename, hiredate FORD 03-DEC-81
13
18-10-2023
NUMERIC Functions
Function Description
MOD() Returns remainder
select mod(100, 7);
2
POWER()/ Returns first argument raised to the power second
POW() select pow(2, 5);
32
ROUND() Rounds to specified no of decimal place or to integer, 2nd input is optional
select (22/7);
3.1429
select round(22/7, 3);
3.143
select round(22/7);
3
14
18-10-2023
NUMERIC Functions
Function Description
SQRT() Returns square root of positive input
select sqrt(6.25);
2.5
select sqrt(11);
3.3166247903554
select sqrt(4);
2
select sqrt(-4);
NULL
TRUNCATE() Truncates to specified number of decimal place or to integer, no
rounding, 2nd input is not optional
select truncate(22/7, 3);
3.142
STRING Functions
15
18-10-2023
STRING Functions
Function Description Example with output
LENGTH(string) Return the number of characters in SELECT LENGTH("Information");
the specified string Output:
11
STRING Functions
Function Description Example with output
LTRIM(string) Returns the given string after SELECT LENGTH(“ DELHI”), LENGTH(LTRIM(“ DELHI”));
removing leading white Output:
space characters +--------+--------+
|7|5|
+--------+--------+
RTRIM(string) Returns the given string after SELECT LENGTH(“PEN “), LENGTH(RTRIM(“PEN “));
removing trailing white space Output:
characters. +--------+--------+
|5|3|
+--------+--------+
TRIM(string) Returns the given string after SELECT LENGTH(“ MADAM “), LENGTH(TRIM(“ MADAM “));
removing both leading and Output:
trailing white space +--------+--------+
characters |9|5|
+--------+--------+
16
18-10-2023
STRING Functions
Function Description
CONCAT() Returns concatenated string
select concat('abc', "pqr", '123');
abcpqr123
SUBSTRING()/ Returns substring
SUBSTR() select substr('abcdefgh', 2, 4);
bcde
17
18-10-2023
DAY(date) It returns the day part from the SELECT DAY("2003-03- 24");
date Output:
24
Function Description
CURDATE()/ Returns current date
CURRENT_DATE()/
CURRENT_DATE
DATE() Returns date part of a date or date-
time input
MONTH() Returns month of an input date
YEAR() Returns year of an input date
NOW() Returns the time at which now
executes
SYSDATE() Returns the current date and time
18
18-10-2023
Aggregate/Statistical function
Statistical function
• avg
• sum General syntax:
• min Function ([DISTINCT | ALL] field/parameter)
• max from table_name;
• count
• count(*)
SUM(SAL)
Select sum(sal)
from emp 10875
where deptno = 20;
Aggregate/Statistical function
19
18-10-2023
20
18-10-2023
21
18-10-2023
Example
Calculate GST as 12 per cent of Price and display the result after rounding it off to one decimal
place.
Add a new column FinalPrice to the table inventory which will have the value as sum of Price and
12 per cent of the GST.
Calculate and display the amount to be paid each month (in multiples of 1000) which is to be
calculated after dividing the FinalPrice of the car into 10 instalments
After dividing the amount into EMIs, find out the remaining amount to be paid immediately, by
performing modular division.
22
18-10-2023
Example
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.
Let us now calculate commission for sales agents as 12% of the SalePrice, Insert the values to the
newly added column Commission and then display records of the table SALE where commission >
73000.
Display InvoiceNo, SalePrice and Commission such that commission value is rounded off to 0.
Example
SELECT LOWER(CustName), UPPER(Email) FROM CUSTOMER;
23
18-10-2023
24
18-10-2023
25
18-10-2023
26
18-10-2023
27