Practical File (SQL)
Practical File (SQL)
PRACTICAL FILE
Advanced Database Management System.
(MYSQL)
(CS-G553)
Submitted by
Shilpa Patial
Registration No: - 2011PGCS015
Programme: - M.tech (CSE)
Under the Guidance of
Assistant Professor: - Ms. Vibhuti Sikri.
INDEX
S.No
Program
Page No.
1.
Basics of SQL.
3-5
2.
Functions in SQL.
6-26
3.
Joins in SQL.
27-32
Signature
database.
SQL Commands:
The standard SQL commands to interact with relational databases are
CREATE, SELECT, INSERT, UPDATE, DELETE, and DROP.
These commands can be classified into groups based on their nature:
Data
Definition
Command
Language:
CREATE
Description
ALTER
DROP
DataManipulationLanguage:
Command
INSERT
UPDATE
DELETE
Description
Creates a record.
Modifies records.
Deletes records.
SQL Syntax:SQL is followed by unique set of rules and guidelines called Syntax.
SQL CREATE DATABASE Statement: CREATE DATABASE
database_name;
Example: - create database APS;
SQL USE Statement: USE DATABASE database_name;
Example:-Use APS;
SQL DROP DATABASE Statement:
database_name;
Example- Drop Database APS;
DROP
DATABASE
Functions in MySQL
SQL has many built-in functions for performing processing on string
or numeric data. Following is the list of all useful SQL built-in
functions:
SQL COUNT Function - The SQL COUNT aggregate function is
used to count the number of rows in a database table.
SQL MAX Function - The SQL MAX aggregate function allows us to
select the highest (maximum) value for a certain column.
SQL MIN Function - The SQL MIN aggregate function allows us to
select the lowest (minimum) value for a certain column.
SQL AVG Function - The SQL AVG aggregate function selects the
average value for certain table column.
SQL SUM Function - The SQL SUM aggregate function allows
selecting the total for a numeric column.
SQL SQRT Functions - This is used to generate a square root of a
given number.
SQL RAND Function - This is used to generate a random number
using SQL command.
SQL CONCAT Function - This is used to concatenate any string
inside any SQL command.
SQL Numeric Functions - Complete list of SQL functions required to
manipulate numbers in SQL.
SQL String Functions - Complete list of SQL functions required to
manipulate strings in SQL.
Returns the
expression.
ACOS()
ASIN()
ATAN()
CEIL()
CEILING()
CONV()
COS()
COT()
DEGREES()
EXP()
FLOOR()
FORMAT()
absolute
value
of
numeric
GREATEST()
Returns the
expressions.
LEAST()
LOG()
LOG10()
MOD()
OCT()
PI()
POW()
POWER()
RADIANS()
ROUND()
SIN()
SQRT()
STD()
STDDEV()
largest
value
of
the
input
expression
TAN()
TRUNCATE()
ABS(X)
ACOS(X)
This function returns the arccosine of X. The value of X must
range between .1 and 1 or NULL will be returned. Consider the
following example:
ASIN(X)
The ASIN () function returns the arcsine of X. The value of X
must be in the range of .1 to 1 or NULL is returned.
ATAN(X)
This function returns the arctangent of X.
ATAN2(Y,X)
This function returns the arctangent of the two arguments: X and
Y. It is similar to the arctangent of Y/X, except that the signs of
both are used to find the quadrant of the result.
CEILING(X)
These function return the smallest integer value that is not
smaller than X. Consider the following example:
CONV(N,from_base,to_base)
The purpose of the CONV() function is to convert numbers
between different number bases. The function returns a string of
the value N converted from from_base to to_base. The
minimum base value is 2 and the maximum is 36. If any of the
arguments are NULL, then the function returns NULL. Consider
the following example, which converts the number 5 from base
16 to base 2:
11
COS(X)
This function returns the cosine of X. The value of X is given in
radians.
SQL>SELECT COS(90);
+---------------------------------------------------------+
| COS(90)
|
+---------------------------------------------------------+
| -0.44807361612917
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
COT(X)
This function returns the cotangent of X. Consider the following
example:
SQL>SELECT COT(1);
+---------------------------------------------------------+
| COT(1)
|
+---------------------------------------------------------+
| 0.64209261593433
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
DEGREES(X)
This function returns the value of X converted from radians to
degrees.
SQL>SELECT DEGREES(PI());
+---------------------------------------------------------+
| DEGREES(PI())
|
+---------------------------------------------------------+
| 180.000000
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
EXP(X)
This function returns the value of e (the base of the natural
logarithm) raised to the power of X.
12
SQL>SELECT EXP(3);
+---------------------------------------------------------+
| EXP(3)
|
+---------------------------------------------------------+
| 20.085537
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
FLOOR(X)
This function returns the largest integer value that is not greater
than X.
SQL>SELECT FLOOR(7.55);
+---------------------------------------------------------+
| FLOOR(7.55)
|
+---------------------------------------------------------+
|7
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
FORMAT(X,D)
The FORMAT() function is used to format the number X in the
following format: ###,###,###.## truncated to D decimal
places. The following example demonstrates the use and output
of the FORMAT() function:
SQL>SELECT FORMAT(423423234.65434453,2);
+---------------------------------------------------------+
| FORMAT(423423234.65434453,2)
|
+---------------------------------------------------------+
| 423,423,234.65
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
GREATEST (n1,n2,n3,..........)
The GREATEST() function returns the greatest value in the set
of input parameters (n1, n2, n3, a nd so on). The following
13
LOG(X)
LOG(B,X)
The single argument version of the function will return the
natural logarithm of X. If it is called with two arguments, it
returns the logarithm of X for an arbitrary base B. Consider the
following example:
SQL>SELECT LOG(45);
+---------------------------------------------------------+
| LOG(45)
|
+---------------------------------------------------------+
| 3.806662
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
14
SQL>SELECT LOG(2,65536);
+---------------------------------------------------------+
| LOG(2,65536)
|
+---------------------------------------------------------+
| 16.000000
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
LOG10(X)
This function returns the base-10 logarithm of X.
SQL>SELECT LOG10(100);
+---------------------------------------------------------+
| LOG10(100)
|
+---------------------------------------------------------+
| 2.000000
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
MOD(N,M)
This function returns the remainder of N divided by M.
Consider the following example:
SQL>SELECT MOD(29,3);
+---------------------------------------------------------+
| MOD(29,3)
|
+---------------------------------------------------------+
|2
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
POW(X,Y)
POWER(X,Y)
These two functions return the value of X raised to the power of
Y.
15
RADIANS(X)
This function returns the value of X, converted from degrees to
radians.
SQL>SELECT RADIANS(90);
+---------------------------------------------------------+
| RADIANS(90)
|
+---------------------------------------------------------+
|1.570796
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
ROUND(X)
ROUND(X,D)
This function returns X rounded to the nearest integer. If a
second argument, D, is supplied, then the function returns X
rounded to D decimal places. D must be positive or all digits to
the right of the decimal point will be removed. Consider the
following example:
SQL>SELECT ROUND(5.693893);
+---------------------------------------------------------+
| ROUND(5.693893)
|
+---------------------------------------------------------+
|6
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
SQL>SELECT ROUND(5.693893,2);
+---------------------------------------------------------+
16
| ROUND(5.693893,2)
|
+---------------------------------------------------------+
| 5.69
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
SIGN(X)
This function returns the sign of X (negative, zero, or positive)
as .1, 0, or 1.
SQL>SELECT SIGN(-4.65);
+---------------------------------------------------------+
| SIGN(-4.65)
|
+---------------------------------------------------------+
| -1
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
SIN(X)
This function returns the sine of X. Consider the following
example:
SQL>SELECT SIN(90);
+---------------------------------------------------------+
| SIN(90)
|
+---------------------------------------------------------+
| 0.893997
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
SQRT(X)
This function returns the non-negative square root of X.
Consider the following example:
SQL>SELECT SQRT(49);
+---------------------------------------------------------+
| SQRT(49)
|
17
+---------------------------------------------------------+
|7
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
TAN(X)
This function returns the tangent of the argument X, which is
expressed in radians.
SQL>SELECT TAN(45);
+---------------------------------------------------------+
| TAN(45)
|
+---------------------------------------------------------+
| 1.619775
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
TRUNCATE(X,D)
This function is used to return the value of X truncated to D
number of decimal places. If D is 0, then the decimal point is
removed. If D is negative, then D number of values in the
integer part of the value is truncated. Consider the following
example:
SQL>SELECT TRUNCATE(7.536432,2);
+---------------------------------------------------------+
| TRUNCATE(7.536432,2)
|
+---------------------------------------------------------+
| 7.53
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
18
SQL string functions are used primarily for string manipulation. The
following table details the important string functions:
Name
Description
ASCII()
BIN()
CHAR()
CONCAT()
FORMAT()
HEX()
INSERT()
LCASE()
LEFT()
LENGTH()
LOWER()
LTRIM()
MID()
REPEAT()
REPLACE()
Replace occurrences
specified string
REVERSE()
RIGHT()
STRCMP()
TRIM()
Remove
spaces
UCASE()
UPPER()
Convert to uppercase
leading
and
of
trailing
ASCII (str)
Returns the numeric value of the leftmost character of the string str.
Returns 0 if str is the empty string. Returns NULL if str is NULL.
ASCII() works for characters with numeric values from 0 to 255.
SQL> SELECT ASCII('2');
+---------------------------------------------------------+
| ASCII('2')
|
+---------------------------------------------------------+
| 50
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
SQL> SELECT ASCII('dx');
+---------------------------------------------------------+
| ASCII('dx')
|
+---------------------------------------------------------+
| 100
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
20
BIN(N)
Returns a string representation of the binary value of N, where N is a
longlong (BIGINT) number. This is equivalent to CONV(N,10,2).
Returns NULL if N is NULL.
SQL> SELECT BIN(12);
+---------------------------------------------------------+
| BIN(12)
|
+---------------------------------------------------------+
| 1100
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
CHAR(N,... [USING charset_name])
CHAR() interprets each argument N as an integer and returns a string
consisting of the characters given by the code values of those integers.
NULL values are skipped.
SQL> SELECT CHAR(77,121,83,81,'76');
+---------------------------------------------------------+
| CHAR(77,121,83,81,'76')
|
+---------------------------------------------------------+
| SQL
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
CONCAT(str1,str2,...)
Returns the string that results from concatenating the arguments. May
have one or more arguments. If all arguments are non-binary strings,
the result is a non-binary string. If the arguments include any binary
strings, the result is a binary string. A numeric argument is converted
to its equivalent binary string form; if you want to avoid that, you can
use an explicit type cast, as in this example:
21
LENGTH(str)
Returns the length of the string str, measured in bytes. A multi-byte
character counts as multiple bytes. This means that for a string
containing five two-byte characters, LENGTH() returns 10, whereas
CHAR_LENGTH() returns 5.
SQL> SELECT LENGTH('text');
+---------------------------------------------------------+
| LENGTH('text')
|
+---------------------------------------------------------+
|4
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
LOWER(str)
Returns the string str with all characters changed to lowercase
according to the current character set mapping.
SQL> SELECT LOWER('QUADRATICALLY');
+---------------------------------------------------------+
| LOWER('QUADRATICALLY')
+---------------------------------------------------------+
| quadratically
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str
replaced by the string to_str. REPLACE() performs a case-sensitive
match when searching for from_str.
23
| SELECT SPACE(6)
|
+---------------------------------------------------------+
|' '
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
STRCMP(str1, str2)
Compares two strings and returns 0 if both strings are equal, it returns
-1 if the first argument is smaller than the second according to the
current sort order otherwise it returns 1.
SQL> SELECT STRCMP('MOHD', 'MOHD');
+---------------------------------------------------------+
| STRCMP('MOHD', 'MOHD')
|
+---------------------------------------------------------+
|0
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
SQL> SELECT SUBSTRING('foobarbar' FROM 4);
+---------------------------------------------------------+
| SUBSTRING('foobarbar' FROM 4)
|
+---------------------------------------------------------+
| barbar
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
UCASE(str)
UCASE() is a synonym for UPPER().
UPPER(str)
Returns the string str with all characters changed to uppercase
according to the current character set mapping.
SQL> SELECT UPPER('Allah');
25
+---------------------------------------------------------+
| UPPER('Allah')
|
+---------------------------------------------------------+
| ALLAH
|
+---------------------------------------------------------+
1 row in set (0.00 sec)
JOIN IN SQL
The SQL Joins clause is used to combine records from two or more
tables in a database. A JOIN is a means for combining fields from two
tables by using values common to each.
Now let us join these two tables in our SELECT statement as follows:
SQL> SELECT ID, NAME, AGE, AMOUNT,
FROM CUSTOMERS, ORDERS
WHERECUSTOMERS.ID = ORDERS.CUSTOMER_ID;
This would produce following result:
+----+---------+-----+--------+
| ID | NAME | AGE | AMOUNT |
+----+---------+-----+--------+
| 3 | kaushik | 23 | 3000 |
| 3 | kaushik | 23 | 1500 |
| 2 | Ramesh | 25 | 1560 |
| 4 | kaushik | 25 | 2060 |
+----+---------+-----+--------+
Here it is noteable that the join is performed in the WHERE clause.
Several operators can be used to join tables, such as =, <, >, <>, <=,
26
>=, !=, BETWEEN, LIKE, and NOT; they can all be used to join
tables. However, the most common operator is the equal symbol.
SQL Join Types:
There are different type of joins available in SQL:
INNER JOIN: returns rows when there is a match in both tables.
LEFT JOIN: returns all rows from the left table, even if there are no
matches in the right table.
RIGHT JOIN: returns all rows from the right table, even if there are
no matches in the left table.
FULL JOIN: returns rows when there is a match in one of the tables.
SELF JOIN: is used to join a table to itself, as if the table were two
tables, temporarily renaming at least one table in the SQL statement.
CARTESIAN JOIN: returns the cartesian product of the sets of
records from the two or more joined tables.
The SQL UNION clause/operator is used to combine the results of
two or more SELECT statements without returning any duplicate
rows.
To use UNION, each SELECT must have the same number of
columns selected, the same number of column expressions, the same
data type, and have them in the same order but they do not have to be
the same length.
Syntax:
The basic syntax of UNION is as follows:
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
27
UNION
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Here given condition could be any given expression based on your
requirement.
Example:
Consider following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota
| 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP
| 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
(b) Another table is ORDERS as follows:
+-----+---------------------+-------------+--------+
|OID | DATE
| CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08
|
3 | 3000 |
| 100 | 2009-10-08
|
3 | 1500 |
| 101 | 2009-11-20
|
2 | 1560 |
| 103 | 2008-05-20
|
4 | 2060 |
+-----+---------------------+-------------+--------+
28
Now let us join these two tables in our SELECT statement as follows:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
This would produce following result:
+------+---------+--------+---------------------+
| ID | NAME | AMOUNT | DATE
|
+------+---------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Ramesh | 1560 | 2009-11-20 00:00:00 |
| 4 | kaushik | 2060 | 2008-05-20 00:00:00 |
| 1 | Ramesh | NULL | NULL
|
| 5 | Hardik | NULL | NULL
|
| 6 | Komal | NULL | NULL
|
| 7 | Muffy | NULL | NULL
|
+------+---------+--------+---------------------+
The UNION ALL Clause:
The UNION ALL operator is used to combine the results of two
SELECT statements including duplicate rows.
The same rules that apply to UNION apply to the UNION ALL
operator.
29
Syntax:
The basic syntax of UNION ALL is as follows:
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
UNION ALL
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Here given condition could be any given expression based on your
requirement.
Example:
Consider following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota
| 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP
| 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
(b) Another table is ORDERS as follows:
+-----+---------------------+-------------+--------+
|OID | DATE
| CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
30
31
32