Computer Project
Computer Project
Signature
Internal Examiner:
External Examiner:
Command :
CREATE TABLE
Employee (
ECODE integer ,
ENAME
Varchar (20),
GENDER
Char (1),
GRADE
Char (2),
GROSS
Integer);
OR
INSERT INTO employee (ECODE, ENAME, GENDER, GRADE, GROSS)
VALUES (1001, ‘Ravi’,
EMPLOYEE
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1002 Akash M A1 35000
1004 NULL M B2 38965
FROM EMPLOYEE;
FROM EMPLOYEE;
Command is:
M
F
Purpose.
FROM EMPLOYEE;
10. CONDITION BASED ON A RANGE
- The BETWEEN operator defines a range of values that the column
values must fall in to make the condition True. The range includes
both lower value and upper value.
Command is:
Ravi
Ruby
e.g. STUDENT
1 ARUN NULL
2 RAVI 56
4 SANJAY NULL
Output will be :
Name
ARUN
SANJAY
ENAME desc;
Output will be:
ENAME
Ravi
Ruby
Neema
UPDATE EMPLOYEE
OTHER EXAMPLES
UPDATE EMPLOYEE
Change the grade to ‘A2’ for those employees whose employee code
is 1004 and name is Neela.
UPDATE EMPLOYEE
SET GRADE=’A2’
That table. After this command is executed, all the data in the table
along with table structure will be deleted.
5. Enabling/Disabling constraints.
E.g. Given a table namely Test with the following data in it.
Col1 Col2
1 A
2 G
Now following commands are given for the table. Predict the table
contents after each of the following statements:
(iii) ALTER TABLE test ADD col5 CHAR (3) NOT NULL;
Table: EMPL
EMPNO ENAME JOB SAL DEPTNO
8369 SMITH CLERK 2985 10
8499 ANYA SALESMAN 9870 20
8566 AMIR SALESMAN 8760 30
8698 BINA MANAGER 5643 20
8912 SUR NULL 3000 10
1. AVG ( )
This function computes the average of given data.
Output
AVG (SAL)
6051.6
2. COUNT ( )
This function counts the number of rows in a given column.
If you specify the asterisk (*), this function returns all rows, including
duplicates and nulls.
Output
COUNT (*)
3. MAX ( )
This function returns the maximum value from a given column or
expression.
Output
MAX (SAL)
9870
4. MIN ( )
This function returns the minimum value from a given column or
expression.
E.g. SELECT MIN (SAL) FROM EMPL;
Output
MIN (SAL)
2985
5. SUM ( )
This function returns the sum of values in given column or
expression.
Output
SUM (SAL)
30258
GROUPING RESULT – GROUP BY
The GROUP BY clause combines all those records (row) that have
identical values in a particular field (column) or a Group of fields
(columns).
Table: EMPL
EMPNO ENAME JOB SAL DEPTNO
8369 SMITH CLERK 2985 10
8499 ANYA SALESMAN 9870 20
8566 AMIR SALESMAN 8760 30
8698 BINA MANAGER 5643 20
Output
CLERK 1
SALESMAN 2
MANAGER 1
Output
DEPTNO SUM(SAL)
10 2985
20 15513
30 8760
NESTED GROUP
- To create a group within a group i.e., nested group, you
need to specify multiple fields in the GROUP BY
Expression.
E.g. To group records job wise within Deptno wise, you need to issue
a query statement like:
Output
DEPTNO JOB COUNT(EMPNO)
10 CLERK 1
20 SALESMAN 1
20 MANAGER 1
30 SALESMAN 1
PLACING CONDITION ON GROUPS – HAVING
CLAUSE
- The HAVING clause places conditions on groups in contrast to
WHERE clause that places condition on individual rows. While
WHERE conditions cannot include aggregate functions, HAVING
conditions can do so.
- E.g. To display the jobs where the number of employees is less than
2,
SELECT JOB, COUNT (*) FROM EMPL GROUP BY JOB HAVING COUNT
(*) < 2;
Output
JOB COUNT(*)
CLERK 1
MANAGER 1
MySQL FUNCTIONS
Table: EMPL
EMPNO ENAME JOB SAL DEPTNO
CONCAT(EMPNO , ENAME)
8369SMITH
8912SUR
Output
LOWER(ENAME)
Smith
Anya
Amir
Bina
Sur
3. UPPER ( ) / UCASE ( )
- Returns the argument in Uppercase. Syntax: UPPER (Column
name)
Output
UPPER(ENAME)
SMITH
ANYA
BINA
AMIR
SUR
4. SUBSTRING ( ) / SUBSTR ( )
Returns the substring as specified.
Syntax:
Output
SUBSTR(ENAME,2,2)
NY
IN
Output
SUBSTR (JOB,-4,2)
SM
AG
Output
RDBMS MySQL
Output
RDBMS MySQL
Output
TRIM (‘RDBMS MySQL’)
RDBMS MySQL
Output
LENGTH (“CANDID”)
LENGTH (ENAME)
5
4
4
4
3
Output
10. RIGHT ( )
– Returns the rightmost number of characters as specified.
E.g. SELECT RIGHT (‘CORPORATE FLOOR’, 3);
Output
11. MID ( ) –
This function is same as SUBSTRING ( ) / SUBSTR ( ) function.
Output
MID (“ABCDEF”, 2, 4)
BCDE
NUMERIC FUNCTIONS
These functions accept numeric values and after
performing the operation, return numeric value.
1. MOD ( )
Returns the remainder of given two numbers.
Output
MOD (11, 4)
3
2. POW ( ) / POWER ( ) –
This function returns MN or a number raised to the n Th power.
Output
POWER (3, 2)
9
3. ROUND ( ) –
This function returns a number rounded off as per given
specifications.
Output
ROUND (15.193, 1)
15.2
Output
SIGN (-15)
-1
Output
SQRT (25)
5
Output
TRUNCATE (15.79, 1)
15.7
Output
CURDATE ( )
2016-12-13
Output
DATE (‘2016-02-09’)
09
Output
MONTH (‘2016-02-09’)
02
Output
YEAR (‘2016-02-09’)
2016
Output
DAYNAME (‘2016-12-14’)
Wednesday
Output
DAYOFMONTH (‘2016-12-14’)
14
Output
DAYOFWEEK (‘2016-12-14’)
Output
DAYOFYEAR (‘2016-02-04’)
35
JOINS
- A join is a query that combines rows from two or
more tables. In a join- query, more than one table are listed in FROM
clause.
Table: empl
EMPNO ENAME JOB SAL DEPTNO
8369 SMITH CLERK 2985 10
8499 ANYA SALESMAN 9870 20
8566 AMIR SALESMAN 8760 30
8698 BINA MANAGER 5643 20
Table: dept
DEPTNO DNAME LOC
10 ACCOUNTING NEW DELHI
20 RESEARCH CHENNAI
30 SALES KOLKATA
40 OPERATIONS MUMBAI
CARTESIAN PRODUCT/UNRESTRICTED
JOIN/CROSS JOIN
- Consider the following query:
This query will give you the Cartesian product i.e. all possible
concatenations are formed of all rows of both the tables EMPL and
DEPT. Such an operation is also known as Unrestricted Join. It returns
n1 x n2 rows where n1 is number of rows in first table and n2 is
number of rows in second table.
EQUI-JOIN
- The join in which columns are compared for
equality, is called Equi- Join. In equi-join, all the columns from joining
table appear in the output even if they are identical.
WHERE EMPL.DEPTNO=DEPT.DEPTNO
ORDER BY EMPL.DEPTNO;
QUALIFIED NAMES
Did you notice that in the entire WHERE conditions of join queries
given so far, the field (column) names are given as.
TABLE ALIAS
- A table alias is a temporary label given along with table name in
FROM clause.
NATURAL JOIN
By default, the results of an equijoin contain two identical
columns. One of the two identical columns can be eliminated by
restating the query. This result is called a Natural join.
E.g. SELECT empl.*, dname, loc FROM empl, dept WHERE
empl.deptno = dept.deptno ;
empl.* means select all columns from empl table. This thing can be
used with any table.
LEFT JOIN
- You can use LEFT JOIN clause in SELECT to produce left
join i.e.
- When using LEFT JOIN all rows from the first table will be returned
whether there are matches in the second table or not. For
unmatched rows of first table, NULL is shown in columns of second
table.
TABLE S1
Roll no Name
1 A
2 B
3 C
4 D
5 E
6 F
TABLE S2
Roll no Class
2 III
4 IX
1 IV
3 V
7 I
8 II
RIGHT JOIN
- It works just like LEFT JOIN but with table order
reversed. All rows from the second table are going to be returned
whether or not there are matches in the first table.