SQL 1
SQL 1
SQL 1
using SQL
Database
UCASE() / UPPER()
LCASE() / LOWER()
MID() / SUBSTRING() / SUBSTR()
LENGTH()
LEFT() ,RIGHT()
INSTR()
LTRIM(),RTRIM()
TRIM()
UCASE() / UPPER()
it converts to upper case
Examples:
1. SELECT UPPER(„Large‟);
2. SELECT UCASE(„Large‟);
LOWER() / LCASE()
it converts to LOWER case
Examples:
1. SELECT LOWER(„LOWER‟);
2. SELECT LCASE(„LOWER‟);
MID() / SUBSTRING() / SUBSTR()
Returns a substring starting from the
specified position.
Examples
1. SELECT
SUBSTRING(„QUADRATICALLY‟,5,6);
2. SELECT SUBSTR(„QUADRATICALLY‟,5,6);
3. SELECT MID(„QUADRATICALLY‟,5,6);
LENGTH()
Returns the length of the string in bytes
Example:
1.SELECT LENGTH(„CANDLE‟);
LEFT()
Returns the LEFT most number of characters as
specified
Example:
1. SELECT LEFT(„USS/234/78‟,3);
RIGHT()
Returns the RIGHT most number of characters as
specified
Example:
1. SELECT RIGHT(„USS/234/78‟,2);
INSTR()
returns the index of the first occurrence of
substring
Example:
1.SELECT INSTR(„CORPORATE FLOOR‟,‟OR‟);
LTRIM()
removes leading spaces
Example:
SELECT LTRIM(„ RDBMS MYSQL‟);
RTRIM
removes trailing spaces
Example:
SELECT RTRIM(„ RDBMS MYSQL „);
1. MATH FUNCTIONS/
MATHEMATICAL/NUMERIC
FUNCTIONS
Mathematical functions perform
mathematical operations on numeric values.
The most commonly used mathematical
functions are:-
POW()/POWER(), MOD(), ROUND().
a) POW()/ POWER()
It returns the argument raised to the
specified power.
E.g.:- select power(2,2); 4
select power(6,3); 72
select power(2,-2); 0.25
b. ROUND()
ROUND(x) rounds the argument to the 0 decimal
place where as ROUND(x,d) rounds the argument
to d decimal places. The round() function returns
a number rounded to a certain number of
decimal places.
Syntax:- SELECT ROUND(x,d)
x-Required (It is the field or value to be rounded
off.)
Y-Required(Specifies the number of decimals to
be returned.)
Values to be rounded off
454.352
-2 -1 0 1 2 3 Decimal places
c. MOD()
The MOD() function returns the modulus (reminder) of one
number divided by another.
Syntax:- select MOD(dividend, divisor)
Dividend – Is a number or a numeric expression to divide.
Divisor- Is a number or a numeric expression by which the dividend is
to be divided.
Ex:-select mod(11,3); 2
1. CURDATE()
Returns the current system date.
e.g:- select curdate(); „2023-03-12‟
2. NOW()
Returns the current date and time.
e.g:- select now(); „2023-03-12 13:58:11‟
3. sysdate()
Returns the time at which the function executes.
E,g:-:- select sysdate(); „2023-03-12 13:59:23‟
Date / Time function
4. date()
It extracts the date part of a date or date time expression.
e.g:- select date(„2021-02-03 01:02:03‟);
„2021-02-03 „
5. month()
Returns the month from the date passed.
e.g:- select month(„2021-02-03‟); 2
6. year()
Returns the year from the inputted date.
e,.g:- select year(„2021-02-03‟); 2021
7. dayname()
Returns the name of the weekday.
e.g:- select dayname(„2023-03-12‟); Sunday
Date / Time function
8. dayofmonth()
Returns the day of the month(0-31)
e.g:- select dayofmonth(„2021-02-03‟); 3
9. monthname()
Returns the name of the month (jan,feb,mar…)
e.g:- select monthname(„2021-02-03‟); february
10. dayofyear()
Returns the day of the year(1-366)
e.g:- select dayofyear(„2021-02-03‟); 34
Aggregate functions
Aggregate functions summarize the result of a query and return a single value
calculated from values in a column instead of providing listing of all the rows.
SUM()
This function is used to find the total value of a particular column.
e.g:- select sum(Marks ) from student;
e.g:- select sum(marks) from student where marks>80;
AVG()
This function is used to find the average value of a particular column.
e.g:- select avg(marks) from student;
e.g:- select avg(marks) from student where DOB<„1999/04/12‟;
MAX()
This function is used to find the maximum value of a particular column.
e.g:-select max(marks) from student where Stream=„Science‟;
e.g:- select min(Marks),max(M arks) from student;
MIN()
Is used to find the minimum value of a particular column.
SQL Aggregate Functions
COUNT()
This function is used to find the number of values
(i.e., number of rows) of a particular column.it does
not count null values in that column.
COUNT(*)
This function returns the total number of records of
rows from the table column, it also count null values.
COUNT-DISTINCT
The keywords distinct and count can be used together
to count the number of records excluding duplicate
values.
e.g:- select distinct stream from student;
e.g:- select count(distinct Stream) from student;
1. To show all the information about the student Ankit Sharma.
2. Display the sum of all the marks whose roll number is greater than 5.
3. Display the maximum and minimum marks from students.
4. Display all the details of students whose name ends with „a‟.
5. Display the count of marks whose name starts with „D‟.
6. Display average of marks.
7. Count distinct gender from student table.
8. Display details of students in ascending order of their marks.
9. To display details of studentsbwhose marks is in the range of 80 to 90.(both
values included)
10.Display total number of rows present in the table student;
11.Display the details of youngest student.
12.Display the name of eldest student.
The above query display the roll number, name and marks of
students on the basis of their marks in ascending order.
SORTING IN SQL– ORDER BY
The above query display the roll number, name and marks of
students on the basis of their marks in ascending order.
SORTING IN SQL– ORDER BY
e.g.:- to display the roll number, name and marks of all the
students in descending order of their marks and ascending
order of their names.
Alias name/naming
If we need to fetch a group of rows on the
basis of common values in a column, this
can be done by using group by clause.
It groups the rows together that contain the
same value in a specified column.
We can use the aggregate
functions(count(),max(),min(),avg(),sum())
to work on the grouped values.
Having clause in SQL is used to specify
conditions on the rows with group by clause.
SQL Joins
Join clause is used to combine rows from two or more
tables based on the common field between them.
While querying for a join, more than one table is
considered in from clause..
The process/function of combining data from multiple
tables called a Join.
Types of Join
Cartesian Product(Cross Product)
Equi Join
Inner Join
Outer Join
Self Join
Non-Equi Join
Natural Join
Equi Join
Suytax:- select <column 1>, <column 2>….. From <table1>,<table 2> where
<table1.Primary key column> = <table2.Foreign key column>
mysql>
create table teacher(t_id int primary key,T_name
varchar(20),stud_id integer,foreign key(stud_id) references
stud(stud_id));
Query OK, 0 rows affected (1.07 sec)