Acknowledgement
I am, A. Jebash Sweety of
class XII – Section B, would like to
express our sincere gratitude to our
computer science teacher Mrs. sudha for
her vital support, guidance and
encouragement – without which this
practical would not have come forth .We
would also like to express our gratitude to
our school VSR international school ,
CERTIFICATE
This is to certify that, Practical on Computer Science is
successfully completed by [Link] SWEETY of Class:
XII, Division: B, Register no………………………….. for the
academic year 2023-2024.
Signature
Internal Examiner:
External Examiner:
Principal: Date: / / 2023
1. CREATING TABLES IN MYSQL
E.g. in order to create table EMPLOYEE given below :
We write the following
ECODE ENAME GENDER GRADE GROSS
Command :
CREATE TABLE
Employee (
ECODE integer ,
ENAME
Varchar (20),
GENDER
Char (1),
GRADE
Char (2),
GROSS
Integer);
2. INSERTING DATA INTO TABLE
- E.g. to enter a row into EMPLOYEE table (created above),
we write command as:
INSERT INTO employee VALUES (1001, ‘Ravi’, ‘M’, ‘E4’, 50000);
OR
INSERT INTO employee (ECODE, ENAME, GENDER, GRADE, GROSS)
VALUES (1001, ‘Ravi’,
‘M’, ‘E4’, 50000);
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
In order to insert another row in EMPLOYEE table , we
write again INSERT
Command: INSERT INTO employee
VALUES (1002, ‘Akash’, ‘M’ , ‘A1’ , 35000);
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1002 Akash M A1 35000
3. INSERTING NULL VALUES
- To insert value NULL in a specific column, we can
type NULL without quotes and NULL will be inserted in that column.
E.g. in order to insert NULL value in ENAME column of
above table, we write
INSERT command as:
INSERT INTO EMPLOYEE
VALUES (1004, NULL, ‘M’, ‘B2’, 38965);
ECODE ENAME GENDER GRADE GROSS
1001 Akash M E4 50000
1002 Ravi M A1 35000
1004 NULL M B2 38965
4. SIMPLE QUERY USING SELECT
COMMAND
- The SELECT command is used to pull information from a
table.
- In order to retrieve everything (all columns) from a
table, SELECT command is used as:
SELECT * FROM <table name>;
E.g. In order to retrieve everything from Employee table, we write
SELECT command as:
EMPLOYEE
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1002 Akash M A1 35000
1004 NULL M B2 38965
SELECT * FROM Employee;
5. SELECTING PARTICULAR COLUMNS
EMPLOYEE
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1002 Akash M A1 35000
1004 Neela F B2 38965
1005 Sunny M A2 30000
1006 Ruby F A1 45000
1009 Neema F A2 52000
- A particular column from a table can be selected by
specifying column-names with SELECT command.
E. g. In above table, if we want to select ECODE and ENAME
column, then command is:
SELECT ECODE, ENAME
FROM EMPLOYEE;
E.g.2 in order to select only ENAME, GRADE and GROSS column,
the command is:
SELECT ENAME, GRADE, GROSS
FROM EMPLOYEE;
6. SELECTING PARTICULAR ROWS
We can select particular rows from a table by specifying a condition
through WHERE clause along with
SELECT statement. E.g. In employee table if we want to select rows
where Gender is female, then
Command is:
SELECT * FROM EMPLOYEE
WHERE GENDER = ‘F’;
E.g.2. in order to select rows where salary is greater than 48000,
then
Command is: SELECT * FROM EMPLOYEE
WHERE GROSS > 48000;
7. ELIMINATING REDUNDANT DATA
The DISTINCT keyword eliminates duplicate rows from the results of
a SELECT statement. For example,
SELECT GENDER FROM EMPLOYEE;
GENDER
M
M
F
M
F
F
SELECT DISTINCT (GENDER) FROM EMPLOYEE;
DISTINCT (GENDER)
M
F
8. VIEWING STRUCTURE OF A TABLE
- If we want to know the structure of a table, we can use DESCRIBE or
DESC command, as per following syntax:
DESCRIBE | DESC <table name>;
E.g. to view the structure of table EMPLOYEE, commands: DESCRIBE
EMPLOYEE; OR DESC EMPLOYEE;
9. USING COLUMN ALIASES
- The columns that we select in a query can be given a different
name, i.e. column alias name for output
Purpose.
E.g. In output, suppose we want to display ECODE column as
EMPLOYEE_CODE in output, then
Command is : SELECT ECODE AS “EMPLOYEE_CODE”
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.
E.g. to display ECODE, ENAME and GRADE of those employees whose
salary is between 40000 and 50000,
Command is:
SELECT ECODE, ENAME, GRADE FROM EMPLOYEE
WHERE GROSS BETWEEN 40000 AND 50000;
Output will be:
ECODE ENAME GRADE
1001 Ravi E4
1006 Ruby A1
11. CONDITION BASED ON A LIST
- To specify a list of values, IN operator is used. The IN operator
selects value that match any value in a given list of values. E.g.
SELECT * FROMEMPLOYEE WHEREGRADE IN (‘A1’, ‘A2’);
Output will be:
ECODE ENAME GENDER GRADE GROSS
1002 Akash M A1 35000
1006 Ruby F A1 45000
1005 Sunny M A2 30000
1009 Neema F A2 52000
- The NOT IN operator finds rows that do not match in the list.
E.g. SELECT * FROM EMPLOYEEWHERE GRADE NOT IN (‘A1’, ‘A2’);
Output will be:
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1004 Neela F B2 38965
12. CONDITION BASED ON PATTERN
MATCHES
- LIKE operator is used for pattern matching in SQL. Patterns are
described using two special wildcard characters:
1. per cent (%) – The % character matches any substring.
2. underscore (_) – The _ character matches any character.
E.g. to display names of employee whose name starts with R in
EMPLOYEE table, the command is:
SELECT ENAME FROM EMPLOYEE WHERE ENAME LIKE ‘R%’;
Output will be:
ENAME
Ravi
Ruby
E.g. to display details of employee whose second character in Name
is ‘e’.
SELECT *FROM EMPLOYEEWHERE ENAME LIKE ‘_e%’;
Output will be:
ECODE ENAME GENDER GRADE GROSS
1004 Neela F B2 38965
1009 Neema F A2 52000
e.g. To display details of employee whose name ends with ‘y’.
SELECT *
FROM EMPLOYEEWHERE ENAME LIKE ‘%y’;
Output will be:
ECODE ENAME GENDER GRADE GROSS
1005 Sunny M A2 30000
1006 Ruby F A1 45000
13. SEARCHING FOR NULL
- The NULL value in a column can be searched for in a table using IS
NULL in the WHERE clause. E.g. to list Employee details whose salary
contains NULL, we use the command:
SELECT *FROM EMPLOYEEWHERE GROSS IS NULL;
e.g. STUDENT
Roll No Name Marks
1 ARUN NULL
2 RAVI 56
4 SANJAY NULL
To display the names of those students whose marks is NULL, we use
the
Command: SELECT Name FROM EMPLOYEEWHERE Marks IS NULL;
Output will be :
Name
ARUN
SANJAY
14. SORTING RESULTS
Whenever the SELECT query is executed, the resulting rows appear in
a predicated order. The ORDER BY Clause allow sorting of query
result. The sorting can be done either in ascending or descending
order, the Default is ascending.
The ORDER BY clause is used as:
SELECT <column name>, <column name>…. FROM <table
name>WHERE <condition>ORDER BY <column name>;
E.g. to display the details of employees in EMPLOYEE table in
alphabetical order, we use command: SELECT *FROM
EMPLOYEEORDER BY ENAME;
Output will be:
ECODE ENAME GENDER GRADE GROSS
1002 Akash M A1 35000
1004 Neela F B2 38965
1009 Neema F A2 52000
1001 Ravi M E4 50000
1006 Ruby F A1 45000
1005 Sunny M A2 30000
E.g. display list of employee in descending alphabetical order whose
salary is greater than 40000.
SELECT ENAME FROMEMPLOYEEWHERE GROSS >40000 ORDER BY
ENAME desc;
Output will be:
ENAME
Ravi
Ruby
Neema
15. MODIFYING DATA IN TABLES
You can modify data in tables using UPDATE command of SQL. The
UPDATE command specifies the rows to be Changed using the
WHERE clause, and the new data using the SET keyword.
E.g. To change the salary of employee of those in EMPLOYEE table
having employee code 1009 to 55000.
UPDATE EMPLOYEE SET GROSS = 55000 WHERE ECODE = 1009;
16. UPDATING MORE THAN ONE
COLUMNS
e.g. to update the salary to 58000 and grade to B2 for those
employee whose employee code is 1001.
UPDATE EMPLOYEE
SET GROSS = 58000, GRADE=’B2’
WHERE ECODE = 1009;
OTHER EXAMPLES
Increase the salary of each employee by 1000 in the EMPLOYEE
table.
UPDATE EMPLOYEE
SET GROSS = GROSS +100;
Double the salary of employees having grade as ‘A1’ or ‘A2’.
UPDATE EMPLOYEE
SET GROSS = GROSS * 2;
WHERE GRADE=’A1’ OR GRADE=’A2’;
Change the grade to ‘A2’ for those employees whose employee code
is 1004 and name is Neela.
UPDATE EMPLOYEE
SET GRADE=’A2’
WHERE ECODE=1004 AND GRADE=’NEELA’;
17. DELETING DATA FROM TABLES
To delete some data from tables, DELETE command is used. The
DELETE command removes rows from a table.
The syntax of DELETE command is:
DELETE FROM <table name> WHERE <condition>;
For example, to remove the details of those employees from
EMPLOYEE table whose grade is A1?
DELETE FROM EMPLOYEE WHERE GRADE =’A1’;
18. TO DELETE ALL THE CONTENTS
FROM A TABLE
DELETE FROM EMPLOYEE; so if we do not specify any condition with
WHERE clause, then all the rows of the table will be deleted Thus
above line will delete all rows from employee table.
19. DROPPING TABLES
The DROP TABLE command lets you drop a table from the database.
E.g. to drop a table employee, we need to write:
DROP TABLE employee;
Once this command is given, the table name is no longer recognized
and no more commands can be given on
That table. After this command is executed, all the data in the table
along with table structure will be deleted.
20. ALTER TABLE COMMAND
The ALTER TABLE command is used to change definitions of existing
tables. (Adding columns, deleting columns etc.). The ALTER TABLE
command is used for:
1. Adding columns to a table
2. Modifying column-definitions of a table.
3. Deleting columns of a table.
4. Adding constraints to table.
5. Enabling/Disabling constraints.
21. ADDING COLUMNS TO TABLE
To add a column to a table this command is used,
e.g. To add a new column ADDRESS to the EMPLOYEE table, we can
write command as:
ALTER TABLE EMPLOYEE
ADD ADDRESS VARCHAR (50);
A new column by the name ADDRESS will be added to
the table, where
Each row will contain NULL value for the new column.
ECODE ENAME GENDER GRADE GROSS ADDRESS
1001 Ravi M E4 50000 NULL
1002 Akash M A1 35000 NULL
1004 Neela F B2 38965 NULL
1005 Sunny M A2 30000 NULL
1006 Ruby F A1 45000 NULL
1009 Neema F A2 52000 NULL
However if you specify NOT NULL constraint while adding a new
column, MySQL adds the new column with the Default value of that
data type e.g. for INT type it will add 0, for CHAR types, it will add a
space, and so on.
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:
(i) ALTER TABLE test ADD col3 INT;
(ii) ALTER TABLE test ADD col4 INT NOT NULL;
(iii) ALTER TABLE test ADD col5 CHAR (3) NOT NULL;
(iv) ALTER TABLE test Tadd col6 VARCHAR (3);
22. MODIFYING COLUMNS
In table EMPLOYEE, change the column GROSS to SALARY.
ALTER TABLE EMPLOYEE
CHANGE GROSS SALARY INTEGER;
In table EMPLOYEE, change the column ENAME to EM_NAME and
data type from VARCHAR (20) to VARCHAR (30).
ALTER TABLE EMPLOYEE
CHANGE ENAME EM_NAME VARCHAR (30);
In table EMPLOYEE, change the data type of GRADE column from
CHAR (2) to VARCHAR (2).
ALTER TABLE EMPLOYEE
MODIFY GRADE VARCHAR (2);
23. DELETING COLUMNS
To delete a column from a table, the ALTER TABLE command takes
the following form:
ALTER TABLE <table name>
DROP <column name>;
E.g. to delete column GRADE from table EMPLOYEE, we will write:
ALTER TABLE EMPLOYEE
DROP GRADE;
24. ADDING/REMOVING CONSTRAINTS
TO A TABLE
ALTER TABLE statement can be used to add constraints to your
existing table.
E.g. to add PRIMARY KEY constraint on column ECODE of table
EMPLOYEE, the command is:
ALTER TABLE EMPLOYEE
ADD PRIMARY KEY (ECODE);
25. REMOVING CONSTRAINTS
- To remove foreign key constraint from table, we use ALTER TABLE
command as:
ALTER TABLE <table name>
DROP FOREIGN KEY;
AGGREGATE / GROUP
FUNCTIONS
Aggregate / Group functions work upon groups of rows, rather than
on single row, and return one single
Output. Different aggregate functions are: COUNT ( ), AVG ( ), MIN
( ), MAX ( ), SUM ( )
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.
E.g. SELECT AVG (SAL) FROM EMPL;
Output
AVG (SAL)
6051.6
2. COUNT ( )
This function counts the number of rows in a given column.
If you specify the COLUMN name in parenthesis of function, then this
function returns rows where COLUMN is not null.
If you specify the asterisk (*), this function returns all rows, including
duplicates and nulls.
E.g. SELECT COUNT (*) FROM EMPL;
Output
COUNT (*)
3. MAX ( )
This function returns the maximum value from a given column or
expression.
E.g. SELECT MAX (SAL) FROM EMPL;
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.
E.g. SELECT SUM (SAL) FROM EMPL;
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).
GROUPING can be done by a column name, or with aggregate
functions in which case the aggregate produces a Value for each
group.
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
E.g. calculate the number of employees in each grade.
SELECT JOB, COUNT (*) FROM EMPLGROUP BY JOB;
Output
JOB COUNT (*)
CLERK 1
SALESMAN 2
MANAGER 1
E.g.2. Calculate the sum of salary for each department.
SELECT DEPTNO, SUM (SAL) FROM EMPL GROUP BY DEPTNO;
Output
DEPTNO SUM(SAL)
10 2985
20 15513
30 8760
E.g.3. find the average salary of each department.
Sol: select avg (sal) FROM EMPLGROUP BY DEPTNO;
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:
SELECT DEPTNO, JOB,COUNT (EMPNO) FROM EMPL GROUP BY
DEPTNO, JOB;
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
Types of MySQL functions: String Functions, Maths Functions and
Date & Time Functions.
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
STRING FUNCTIONS
1. CONCAT ( ) - Returns the Concatenated String.
Syntax: CONCAT (Column1, Column2, Column3,)
E.g. SELECT CONCAT (EMPNO, ENAME) FROM EMPL
WHEREDEPTNO=10;
Output
CONCAT(EMPNO , ENAME)
8369SMITH
8912SUR
2. LOWER ( ) / LCASE ( ) - Returns the Argument in
lowercase.
Syntax: LOWER (Column name)
e.g. SELECT LOWER (ENAME) FROM EMPL;
Output
LOWER(ENAME)
Smith
Anya
Amir
Bina
Sur
3. UPPER ( ) / UCASE ( )
- Returns the argument in Uppercase. Syntax: UPPER (Column
name)
E.g. SELECT UPPER (ENAME) FROM EMPL;
Output
UPPER(ENAME)
SMITH
ANYA
BINA
AMIR
SUR
4. SUBSTRING ( ) / SUBSTR ( )
Returns the substring as specified.
Syntax:
SUBSTR (Column name, m, n), where m specifies starting index
and n specifies number of Characters from the starting index m .e.g.
SELECT SUBSTR (ENAME, 2, 2) FROM EMPL WHERE DEPTNO=20;
Output
SUBSTR(ENAME,2,2)
NY
IN
SELECT SUBSTR (JOB,-2,2) FROMEMPL WHERE DEPTNO=20;
Output
SUBSTR (JOB,-4,2)
SM
AG
5. LTRIM ( ) – Removes leading spaces.
E.g. SELECT LTRIM (‘RDBMS MySQL’);
Output
LTRIM (‘RDBMS MySQL’)
RDBMS MySQL
6. RTRIM ( ) – Removes trailing spaces.
E.g. SELECT RTRIM (‘RDBMS MySQL ’) ;
Output
RTRIM (‘RDBMS MySQL’)
RDBMS MySQL
7. TRIM ( ) – Removes trailing and leading spaces.
E.g. SELECT TRIM (‘RDBMS MySQL ’) ;
Output
TRIM (‘RDBMS MySQL’)
RDBMS MySQL
8. LENGTH ( ) – Returns the length of a string.
E.g. SELECT LENGTH (“CANDID”) ;
Output
LENGTH (“CANDID”)
LENGTH (ENAME)
5
4
4
4
3
9. LEFT ( ) – Returns the leftmost number of characters
as specified.
E.g. SELECT LEFT (‘CORPORATE FLOOR’, 3);
Output
LEFT (‘CORPORATE FLOOR’, 3)
COR
10. RIGHT ( )
– Returns the rightmost number of characters as specified.
E.g. SELECT RIGHT (‘CORPORATE FLOOR’, 3);
Output
RIGHT (‘CORPORATE FLOOR’, 3)
OOR
11. MID ( ) –
This function is same as SUBSTRING ( ) / SUBSTR ( ) function.
E.g. SELECT MID (“ABCDEF”, 2, 4);
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.
E.g. SELECT MOD (11, 4);
Output
MOD (11, 4)
3
2. POW ( ) / POWER ( ) –
This function returns MN or a number raised to the n Th power.
E.g. SELECT POWER (3, 2);
Output
POWER (3, 2)
9
3. ROUND ( ) –
This function returns a number rounded off as per given
specifications.
E.g. ROUND (15.193, 1);
Output
ROUND (15.193, 1)
15.2
4. SIGN ( ) – This function returns sign of a given number.
If number is negative, the function Returns1. If number is positive,
the Function returns 1. If number is zero, the function returns 0.
E.g. SELECT SIGN (-15) ;
Output
SIGN (-15)
-1
5. SQRT ( ) – This function returns the square root of a given
number. E.g. SELECT SQRT (25);
Output
SQRT (25)
5
6. TRUNCATE ( ) – This function returns a number with some
digits truncated. E.g. SELECT TRUNCATE (15.79, 1);
Output
TRUNCATE (15.79, 1)
15.7
DATE AND TIME FUNCTIONS
Date functions operate on values of the DATE data type.
1. CURDATE ( ) / CURRENT_DATE ( ) – This function returns
the current date.
E.g. SELECT CURDATE ( );
Output
CURDATE ( )
2016-12-13
2. DATE ( ) – This function extracts the date part from a date.
E.g. SELECT DATE (‘2016-02-09’) ;
Output
DATE (‘2016-02-09’)
09
3. MONTH ( ) – This function returns the month from the date
passed. E.g. SELECT MONTH (‘2016-02-09’);
Output
MONTH (‘2016-02-09’)
02
4. YEAR ( ) – This function returns the year part of a date.
E.g.
SELECT YEAR (‘2016-02-09’) ;
Output
YEAR (‘2016-02-09’)
2016
5. DAYNAME ( ) – This function returns the name of weekday.
E.g. SELECT DAY NAME (‘2016-02-09’) ;
Output
DAYNAME (‘2016-12-14’)
Wednesday
6. DAYOFMONTH ( ) – This function returns the day of
month. Returns value in range of 1 to 31.
E.g. SELECT DAYOFMONTH (‘2016-12-14’);
Output
DAYOFMONTH (‘2016-12-14’)
14
7. DAYOFWEEK ( ) – This function returns the day of week.
Return the weekday index for date. (1=Sunday, 2=Monday,
7=Saturday)
SELECT DAYOFWEEK (‘2016-12-14’);
Output
DAYOFWEEK (‘2016-12-14’)
8. DAYOFYEAR ( ) – This function returns the day of the year.
Returns the Value between 1 and 366
. E.g. SELECT DAYOFYEAR (‘2016-02-04) ;
Output
DAYOFYEAR (‘2016-02-04’)
35
9. NOW ( ) – This function returns the current date and time.
It returns a constant time that indicates the time at which the
statement began to execute.
E.g. SELECT NOW ( );
10. SYSDATE ( ) – It also returns the current date but it return
the time at which SYSDATE ( ) executes. It Differs from the behaviour
for NOW ( ), which returns a constant time that indicates the time at
which the
Statement began to execute.
E.g. SELECT SYSDATE ( ) ;
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:
SELECT * FROM EMPL, DEPT;
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.
e.g. SELECT * FROM empl, dept WHERE [Link] = [Link] ;
Q: with reference to empl and dept table, find the
location of employee SMITH. Ename column is present in
empl and loc column is present in dept. In order to obtain the result,
we have to join two tables.
SELECT ENAME, LOC
FROM EMPL, DEPT WHERE [Link] = [Link] AND
ENAME=’SMITH’;
Q: Display details like department number, department
name, employee number, employee name, job and
salary. And order the rows by department number.
SELECT [Link], dname, emp no, e name, job, Sal
FROM EMPL, DEPT
WHERE [Link]=[Link]
ORDER BY [Link];
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 name>.<column name>
This type of field names is called qualified field names. Qualified field
names are very useful in identifying a field if the two joining tables
have fields with same time. For example, if we say dept no field from
joining tables empl and dept, you’ll definitely ask- dept no field of
which table? To avoid such an ambiguity, the qualified field names
are used.
TABLE ALIAS
- A table alias is a temporary label given along with table name in
FROM clause.
e.g. SELECT [Link], DNAME, EMPNO, ENAME, JOB, SAL FROM
EMPL E, DEPT DWHERE [Link] = [Link] ORDER BY [Link];
In above command table alias for EMPL table is E and for
DEPT table, alias is D.
Q: Display details like department number, department name,
employee number, employee name, job and salary. And order
the rows by employee number with department number.
These details should be only for employees earning at least Rs.6000
and of SALES department. SELECT [Link], DNAME, EMPNO,
ENAME, JOB, SAL FROM EMPL E, DEPT D WHERE [Link] =
[Link] AND DNAME=’SALES’ AND SAL>=6000 ORDER BY
[Link];
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
[Link] = [Link] ;
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
SELECT S1.ROLL_NO, NAME, CLASS
FROM S1 LEFT JOIN S2 ON S1.ROLL_NO=S2.ROLL_NO;
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.
- You can use RIGHT JOIN in SELECT to produce right join
E.g. SELECT S1.ROLL_NO, NAME, CLASS
FROM S1 RIGHT JOIN S2 ON S1.ROLL_NO=S2.ROLL_NO