Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

ASSIGNMENT 1

AIM:
To create a Data Definition Language (DDL) Command to
perform the creation of a table, alter, modify, and drop
column.
THEORY:
 Creation of table
 Altering table
 Check constraint
 Insertion
 Deletion

Q1) Create a table called EMP with the following structure. Name Type---
--> EMPNO NUMBER(6) ENAME VARCHAR2(20) JOB VARCHAR2(10)
DEPTNO NUMBER(3) SAL NUMBER(7,2) Allow NULL for all columns except
ename and job.

INPUT
Q2) Add a column experience to the emp table. experience numeric null
allowed.

Q3) Modify the column width of the job field of emp table.
INPUT

INSERTING VALUES IN THE TABLE

FINAL TABLE OUTPUT


Q4)Create the emp1 table with ename and empno, add constraints to
check the empnovalue while entering (i.e) empno > 100.

INPUT

INSERTING EMPNO>100

INSERTING EMPNO<100

ERROR IN OUTPUT
Error Code: 3819. Check constraint 'emp1_chk_1' is violated. 0.000 sec

CONCLUSION:
In this assignment, we learned to create a table in SQL, insert
values into it, update the table, and apply a CHECK constraint
to ensure data integrity.
ASSIGNMENT 2
AIM
To learn the utilization of “select, insert, update, delete, drop & alter”
statements.

THEORY
 The INSERT INTO statement is used to insert new records in a table
 The UPDATE statement is used to modify the existing records in a
table.
 The DELETE statement is used to delete the existing records in a
table.
 The DROP statement deletes a column in the existing table.
 The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table.
 The ALTER TABLE statement is also used to add and drop various
constraints on an existing table

Q1) Create a table called EMPLOYEE table with name EMP with the fields
like EMPNO NUMBER(6), ENAME VARCHAR2(20), JOB VARCHAR2(10),
DEPTNO NUMBER(3), SAL NUMBER(7,2) and insert the values.
INPUT
Q2) Write query to select all the columns of emp table.
INPUT

OUTPUT

Q3) Write a query to select only Empname, Ename, and Job.


INPUT

OUTPUT
Q4)Write a query to select unique Jobs.
INPUT

OUTPUT

Q5) Write a query to select only those employees who are salesmen.
INPUT

OUTPUT

Q6) select Ename , Job and salary, in the order of their salary.
INPUT

OUTPUT
Q7) Compare DROP and ALTER statements with proper SQL query and
table and display the output.
Ans
DROP Statement:
The DROP statement deletes an existing database object, such as a table.
Syntax:
DROP TABLE Table_Name;
ALTER Statement:
The ALTER statement is used to modify an existing database table.
Syntax:
ALTER TABLE Table_Name ADD Column Datatype;
INPUT

OUTPUT
ASSIGNMENT 3
AIM
To learn the utilization of Group by, Having, System Functions, Set
Operators
THEORY
 Set operators can be used to combine records from multiple tables.
 ‘Having’ is used to restrict some of the groups appearing in the
result
 String functions.
Existing employees Table:

Q1) List down number of employees, minimum salary , the maximum


salary for each department
INPUT

OUTPUT
Q2) Update Email_id, if department id is
a. < 1000 update the EMAIL field by appending @oracle.com
b. < 5000 update the EMAIL field by appending @oracle.co.uk
c. Else update it as oracle.co.in
INPUT

UPDATED TABLE

Q3) Display a departmentID-wise count of employees


 Getting a salary more than 5000
 Apart from the above condition, select only those departments which
has an average salary in excess of 6500

INPUT OUTPUT
Q4) You want to add a new row in the employees table with employee id 10000,
First Name = ‘Scott’ , Last Name = ‘Tiger’ , Email = Stiger, Hire Date , 01/02/2014,
Job id PR_Prsdnt ( Title ‘Company President’ ) Department_id 280 (
Department_Name ‘Database’ ) Salary 50000 h. Issue necessary insert
statements.

INPUT

Q5) Display day, month and year of the hire date of the employees.
INPUT

OUTPUT

Q6) Apart from ‘Delete’ a ‘Truncate’ statement can also be used for deleting
the rows. Comment on their difference.
Ans: DELETE removes rows individually, can be rolled back in a transaction,
logs individual row deletions, and enforces referential integrity constraints.
TRUNCATE removes all rows at once, cannot be rolled back, generates minimal
logging, and does not enforce referential integrity constraint.
CONCLUSION
In this assignment, we covered the usage of SQL concepts and commands such
as `GROUP BY`, `HAVING`, system functions, and set operators.
ASSIGNMENT 4
AIM
To learn about combining two tables using joins and also learn how to use
sub query.
THEORY
Join
A join query extracts information from two or more tables or views. A join query
differs
from a regular query in at least the following two ways:
The FROM clause of a join query refers to two or more tables or views.
A condition is specified in the join query (known as join condition) that relates
the rows of one table to the rows of another table.
Type of Joins
Inner Join: Inner joins are the regular joins. An inner join returns the rows that
satisfy the
join condition.
Outer Join: Outer joins are an extension to inner joins. An outer join returns the
rows that
satisfy the join condition and also the rows from one table for which no
corresponding rows
(i.e., that satisfy the join condition) exist in the other table.

● FROM table1 {LEFT | RIGHT | FULL} [OUTER] JOIN table2


 Left Outer join:
 Right Outer Join:
 Full outer Join:
 Cartesian join or cross-join
When you don't specify a join condition when joining two tables
Self joins
A self-join is a join of a table to itself.
Equi- and non-equi-joins
An equi-join is a join where the join condition uses the equal
Q1) Display name of employees, department name and job name for each
employee
INPUT

OUTPUT

Q2) Display the department name along with no of employees an average


salary of that department.
INPUT
OUTPUT

Q3) For each department, find out no. of jobs the employees are assigned
to.
INPUT

OUTPUT

Q4) Check for correctness of the above queries in terms of count, if you
want to bring in all entries, how would you achieve the same?
Ans:
The correctness of the above queries depends on the structure and data
in your database. To ensure the accuracy of the queries, you should
consider the following:
1. Data Integrity: Make sure that your data is accurate and consistent.
The queries assume that the data in the "DEPARTMENT,"
"EMPLOYEE," and "JOB" tables is correctly structured.
2. Foreign Key Constraints: Ensure that foreign key constraints are
properly defined to maintain referential integrity between the
tables. This helps avoid issues like referencing non-existent
departments or jobs.
3. Column Names: Double-check that the column names used in the
queries match the actual column names in your database.
4. Data Completeness: Ensure that you have data for all relevant
departments and employees. If any departments or employees are
missing from the data, you may not get accurate results.
5. Data Types: Confirm that the data types used in the queries match
the actual data types of your columns.

CONCLUSION
In this assignment, we learned how to join two tables in SQL, used different
types of joining techniques, and also learned about how to use subquery.
ASSIGNMENT 5
AIM:
i)to learn about utility of JOINS and also sub-query.
ii)to learn about creating views, creating column aliases and uses of
‘grant’ and ‘revoke’ command

THEORY:
Join:
A join query extracts information from two or more tables or views. The
main requirement for a join is that there must be at least one common field
between the tables being joined and that it is bound to have a condition.
These are the types of joins possible:
a) Inner join: joins that strictly satisfy the join condition
b) Outer join: joins that contain tuples that not only satisfy the condition
but also those from any of the tables for which no corresponding tuples
exist in the other table(s) – like left outer join, right outer join, and full outer
join.
c) Self join: joins that involve one table with itself

Subquery:
A subquery is a query that is run within a query. It is used for situations
where the combined data has no direct relationship, unlike joins. The
subquery is run first, followed by the main query during compilation.
View:
A view is a virtual table based on the result-set of an SQL statement.A view
contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
Q1) Create an EMPLOYEE table and display NAME, LOCATION, PHONENO
of an employee from EMPLOYEE table whose belongs to specific
department.

Ans:

INPUT:

OUTPUT:

Q2) Create an "Orders" table with attributes ORDERID, CUSTOMERID and


ORDERDATE & create a “Customers" table with attributes CUSTOMERID,
CUSTOMERNAME, CONTACTNAME and COUNTRY. Create the SQL
statement that selects records that have matching values in both tables
INPUT:

OUTPUT:

Q3) Write down an SQL query to create a view that shows all
customers from a specific location.
Ans:
INPUT:

OUTPUT
Q4. Write down an SQL query to create two aliases, one for the
CustomerID column and one for the CustomerName column.
Ans:

INPUT:

OUTPUT:

Q5) Compare GRANT & REVOKE command with proper examples.


Ans: GRANT and REVOKE are both permission-based commands.
GRANT gives permission for using any particular keyword, to other
user(s) other than the database creator themselves. REVOKE is the
opposite, it removes permission for the use of the keyword.
Syntax for GRANT:
To grant access to ‘SELECT’ object for table named ‘perm’ to abc@123,

Syntax for REVOKE:


To revoke access to SELECT object for table named ‘perm’ to xyz@123,

CONCLUSION
In this assignment, we explored and applied two fundamental SQL
concepts: table join operations and subqueries, which are essential for
working with relational databases and managing complex data
relationships.

You might also like