XII-IP-UNIT-II-Database Query Using SQL
XII-IP-UNIT-II-Database Query Using SQL
Class-12-IP (TERM-II)
MySQL server is a open-source relational database management
system which is a major support for web based applications. Databases
and related tables are the main component of many websites and
applications as the data is stored and exchanged over the web.
MySQL server is used for data operations like querying, sorting, filtering,
grouping, modifying and joining the tables.
Advantages of MySQL :
Fast and high Performance database.
Easy to use, maintain and administer.
Easily available and maintain integrity of database.
Provides scalability, usability and reliability.
Low cost hardware.
MySQL can read simple and complex queries and write operations.
InnoDB is default and widely used storage engine.
Provides strong indexing support.
Provides SSL support for secured connections.
Provides powerful data encryption and accuracy.
Provides Cross-platform compatibility.
Provides minimized code repetition.
FUNCTION-Functions are simply pieces of code that perform
some operations and then return a result. Some functions
accept parameters while other functions do not accept
parameters.
MySQL Math Functions are the MySQL built-in functions which refer the
numeric type functions and commands to operate the mathematical
logics. The Math functions in MySQL are the numeric functions used in
the SQL query commands mainly for the mathematical calculations and
produce the numeric literals as results. These Math Functions perform
numeric handling but if receives an error event during query
implementation then, it returns the NULL value as output.
POW() Function
This POW() function provides the value of one argument passed that is
raised to the power of other argument with numeric values specified
while executing in the server.
Code:
SELECT POW(2,5);
Output:
32
27. POWER() Function
Suppose, this POWER() is passed with two arguments Expr1 and Expr2
then, on implementation it outputs the value of Expr1 which is raised to
the power of Expr2 argument.
Code:
SELECT POWER(3,4);
Output:
81
places is provided for round off, it rounds off the number to the nearest
integer. ... If it is negative, then the number is rounded to the left side of
the decimal point.
SELECT ROUND(10.5);
o/p –
11
SELECT ROUND(10.6);
o/p –
11
SELECT ROUND(-10.5);
o/p—
-11
SELECT ROUND(-10.6);
o/p—
-11
The value of the fractional part less than .5 is rounded down to the next
integer if positive or up to the next integer if negative.
SELECT ROUND(10.4); --
o/p
10
SELECT ROUND(-10.4);
o/p—
-10
mysql> SELECT ROUND(125.315); SELECT ROUND(454.1234,-2);
Result: 125
Result: 500
mysql> SELECT ROUND(125.315, 0);
Result: 125 SELECT ROUND(444.1234,-2);
Result: 400
mysql> SELECT ROUND(125.315, 1);
Result: 125.3
Parameter Description
string Required. The string to extract from
start Required. The start position. Can be both a positive or
negative number. If it is a positive number, this function
extracts from the beginning of the string. If it is a negative
number, this function extracts from the end of the string
length Optional. The number of characters to extract. If omitted, the
whole string will be returned (from the start position)
The SUBSTR() and MID
() functions equals to
the SUBSTRING()
function.
SUBSTR() function
MySQL SUBSTR() returns the specified number of characters from a particular
position of a given string. SUBSTR() is a synonym for SUBSTRING().
Syntax-
SUBSTR(string, start, length)
Parameter Description
string Required. The string to extract from
start Required. The start position. Can be both a positive or negative number.
If it is a positive number, this function extracts from the beginning of
the string. If it is a negative number, this function extracts from the end
of the string
length Optional. The number of characters to extract. If omitted, the whole
string will be returned (from the start position)
LENGTH() Function in MySQL
This function in MySQL is used to find the string length which is of type bytes.
Features :
This function is used to find the string length which is of type bytes.
This function comes under String Functions.
This function accepts only one parameter namely string.
This function returns the length in bytes only.
Syntax-
LENGTH(string)
Returns :
It returns the string length which is of type bytes.
LENGTH( str ) Returns the length of the string str , measured in bytes. A multibyte character
counts as multiple bytes. This means that for a string containing five 2-byte characters,
LENGTH() returns 10 , whereas CHAR_LENGTH() returns 5 .
mysql> SELECT LENGTH('text');
o/p-> 4
mysql> SELECT LENGTH(NULL);
Result: NULL
mysql> SELECT LENGTH('');
Result: 0
mysql> SELECT LENGTH(' ');
Result: 1
mysql> SELECT LENGTH('Tech on the Net');
Result: 15
mysql> SELECT LENGTH('techonthenet.com');
Result: 16
The LEFT() function extracts a number of characters from a string (starting
from left).
Syntax-
LEFT(string, number_of_chars)
Parameter Description
string Required. The string to extract from
number_of_chars Required. The number of characters to extract. If this parameter
is larger than the number of characters in string, this function will
return string
RIGHT() function in MySQL is used to extract a specified number of characters
from the right side of a given string. Second argument is used to decide, how
many characters it should return. Syntax : RIGHT( str, len )
Parameter Description
string Required. The string to extract from
len Required. The number of characters to extract. If this parameter
is larger than the number of characters in string, this function will
return string
MySQL INSTR() Function
The INSTR() function returns the position of the first occurrence of a string
in another string. This function performs a case-insensitive search.
Syntax-
INSTR(string_1, string_2)
This function accepts 2 parameters.
string_1 –
The string where searching takes place.
string_2 –
The string/sub-string which will be searched in string_1.
Returns :
It returns the position of the first occurrence of a substring within a given
string.
Note –
The function will return 0 if
string_2 is not found in string_1.
INSTR() function only performs
case-insensitive searches.
LTRIM() : This function in MySQL
is used to remove leading
spaces from a string.
Syntax-
LTRIM(string)
RTRIM() : This function in MySQL
is used to remove trailing spaces
from a string.
Syntax-
RTRIM(string)
TRIM() function in MySQL
is used to clean up data. It is
also used to remove the
unwanted leading and trailing
characters in a string. Syntax :
TRIM([{BOTH | LEADING |
TRAILING} [remstr] FROM] str)
The DATE() function extracts the date part from a datetime expression.
Syntax-
DATE(expression)
Parameter Description
expression Required. A valid date/datetime value. Returns NULL if expression is
not a date or a datetime
SELECT MIN(column_name)
FROM table_name
WHERE condition;
to calculate the total working hours of employees whose working_hours >= 12.
MySQL count() function is used to returns the count of an expression. It allows us to
count all rows or only some rows of the table that matches a specified condition. It is a
type of aggregate function whose return type is BIGINT.
We can use the count function in three forms, which are explained below:
•Count (*)
•Count (expression)
•Count (distinct)
COUNT(*) Function: This function uses the SELECT statement to returns the count
of rows in a result set. The result set contains all Non-Null, Null, and duplicates
rows.
COUNT(expression) Function: This function returns the result set without containing
Null rows as the result of an expression.
COUNT(distinct expression) Function: This function returns the count of distinct rows
without containing NULL values as the result of the expression.
to calculates the total number of employees name available in the table
specifies the rows whose value in the column emp_age is greater than 32
counts the Non-Null and distinct rows in the column emp_age
SQL commands are mainly categorized into four categories-
DDL – Data Definition Language-[CREATE, DROP, ALTER, TRUNCATE, COMMENT, RENAME]
DQl – Data Query Language- [SELECT]
DML – Data Manipulation Language-[INSERT,UPDATE, DELETE, LOCK, CALL, EXPLAIN PLAN]
DCL – Data Control Language-[GRANT, REVOKE]
The MySQL GROUP BY clause is used in a SELECT statement to collect data
across multiple records and group the results by one or more columns.
The HAVING clause is used in the SELECT statement to specify filter conditions for a
group of rows or aggregates.
The HAVING clause is often used with the GROUP BY clause to filter groups based on a
specified condition. If you omit the GROUP BY clause, the HAVING clause behaves like
the WHERE clause.
Syntax-
SELECT select_list
FROM table_name
WHERE search_condition
GROUP BY group_by_expression
HAVING group_condition;
SELECT expressions
FROM tables
[WHERE conditions]
ORDER BY expression [ ASC | DESC ];
MySQL ORDER BY: without using ASC/DESC attribute
MySQL ORDER BY: with ASC attribute
MySQL ORDER BY: with DESC attribute
MySQL ORDER BY: using both ASC and DESC attributes