Sunteți pe pagina 1din 26

Unit I

DBMS (Database Management System)

DBMS is basically a collection of programs whose primary purpose is to facilitate a user to


insert, update and retrieve data and thus make it easy to design, maintain and access databases.
The dbms is also in charge of access, security, storage and host of other functions for the
database system.

SQL (STRUCTURED QUERY LANGUAGE)

SQL stands for Structured Query Language. SQL is a domain-specific language used in


programming and designed for managing data held in a relational database management
system (RDBMS), or for stream processing in a relational data stream management
system (RDSMS).

 The SQL CREATE DATABASE Statement


The create database statement is used to create a new SQL database.
Syntax:-
create database database name;

 The SQL CREATE TABLE Statement


The CREATE TABLE statement is used to create a new table in a database.
Syntax:-
CREATE TABLE table_name (
     column1 datatype,
     column2 datatype,
     column3 datatype ......);

 Create Table Using another Table


If you create a new table using an existing table, the new table will be filled with the
existing values from the old table.Syntax:-
CREATE TABLE new_table_name AS
     SELECT column1, column2,...
     FROM existing_table_name WHERE ....;

1
LAB QUERY :-
1) Write a query to create tables EMPLOYEE AND DEPARTMENT WITH THE
GIVEN ATTRIBUTES
a) EMPLOYEE(E_ID,FIRST_NAME,MIDDLE_NAME,LAST_NAME,DEPARTMEN
T_Id,
SALARY,PHONE_NO,EMAIL_ADDRESS).
b) DEPARTMENT(DEPT_ID,DEPT_NAME,LOCATION,MANAGER)

OUTPUT:-

a)
2.

Output:- b)
3.

 Select Statement:-In most applications, SELECT is the most commonly


used data
query language command. The SELECT statement is used to
retrieve rows from the table. This statement is a tremendously powerful tool and its syntax is
complicated because of the many ways that tables, columns, function can operators can be
combined into legal statements.The basic syntax of the SELECT statement is as follows:-
SELECT column1,column2, columnN from table_name;
If we want to fetch all the fields of the CUSTOERS table, then you should use the following
query. sql> SELECT * FROM table_name;
o The SQL GROUP BY Clause
The GROUP BY clause is used to group rows with same values. The GROUP BY clause follows the
WHERE clause in a select statement and precedes the ORDER BY clause. SYNTAX :-
SELECT column1,column2
FROM table_name WHERE[conditions]
GROUP BY column1,column2 ORDER BY column1, column2
4.

o The SQL WHERE Clause


The where clause is used to specify a condition while fetching the data from a single table or by
joining with multiple tables. The WHERE clause is not only used in the SELECT statement, but
it is also used in the UPDATE, DELETE statement,etc.
Syntax:-
SELECT column1, cloumn2, column FROM table_name WHERE [condition];

o The SQL SELECT DISTINCT Statement


The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to
list the different (distinct) values.SELECT DISTINCT Syntax:-
SELECT DISTINCT column1, column2, ...FROM table_name;

o The SQL ORDER BY Keyword


The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword. ORDER BY Syntax:-
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;

LAB QUERY
2) Write a query to list all the EMPLOYEE whose name starts with letter ‘l’.

Output:-
5.

3) Find the number of employees working in ‘accounts department.

Output:

4) Find the maximum salary of an employee,employee count in department,sum of


salary of all the employees etc using aggregate function.

Output:-
6.

 SQL ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

 ALTER TABLE - ADD Column


To add a column in a table, Syntax:
ALTER TABLE table_name
ADD column_name datatype;

 ALTER TABLE - DROP COLUMN


To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):Syntax:-
ALTER TABLE table_name
DROP COLUMN column_name;

 ALTER TABLE - ALTER/MODIFY COLUMN


To change the data type of a column in a table,Syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype;

Lab query :-
5) Write a query to add an attribute(column) in the table.

Output:-
7.

6) WRITE A QUERY TO CHANGE THE DATA TYPE AND SIZE OF AN


ATTRIBUTE.

OUTPUT:-
8.
9.

 SQL Constraints
SQL constraints are used to specify rules for the data in a table. Constraints are used to limit the
type of data that can go into a table.

o SQL Create Constraints

Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.Syntax:-

CREATE TABLE table_name (column1 datatype constraint,
    column2 datatype constraint,column3 datatype constraint, ....);

o Types of constraints:

1. SQL NOT NULL Constraint:- Ensures that a column cannot have a NULL value

2. SQL UNIQUE Constraint:- Ensures that all values in a column are different

3. SQL PRIMARY KEY Constraint:- A combination of a NOT NULL and UNIQUE.


Uniquely identifies each row in a table
4. SQL FOREIGN KEY Constraint:- Uniquely identifies a row/record in another table

5. SQL CHECK Constraint:-Ensures that all values in a column satisfies a specific condition

6. SQL DEFAULT Constraint:- Sets a default value for a column when no value is specified

7. SQL INDEX Constraint:- Used to create and retrieve data from the database very quickly

10.

LAB QUERY

7) Write a query to apply following constraints on the table.


 Primary key constraint
 Not null constraint
 Check constraint
 Unique constraint
 Referential integrity constraint.

Output:-

 PRIMARY KEY CONSTRAINT.


11.

 NOT NULL CONSTRAINTS


 CHECK CONSTRAINTS

12.

 UNIQUE CONSTRAINTS
 REFERENTIAL INTEGRITY CONSTRAINT

13.

8. Write a query to find the total salary being paid to each job description in
department you may leave where salary <3000.
Output:-

9. write a query to join EMPLOYEE table and DEPARTMENT table.


Output:-

14.

10. Display the last name, department name, and salary of any employee whose
salary matches the salary of another employee with E_ID 1700.
Output:

15.

Unit II
 Joins
A join is a mechanism that extracts information from one or more tables. It allows retrieval of
rows from two or more related tables that share a common set of values.
Syntax :- SELECT column-names FROM table-name1 JOIN table-name2
ON column-name1 = column-name2 WHERE condition

 Types of joins:-

i. Equi join
ii. Cartesian product
iii. Outer join
iv. Self join
v. Inner join

i. Equi join
The EQUIJOIN or INNER JOIN is a join in which join condition contains an
equality operator(=). It combine rows that have equivalent values for the specified
columns.syntax:- SELECT column_list FROM table1, table2....
WHERE table1.column_name = table2.column_name;

16.

ii. Cartesian product


If the join condition is omitted from the join query, then the reqult is a Cartesian
product. In the Cartesian product, each row of one table is joined to every row of
another of another table.it is also called cross join.
Syntax:- Syntax:- SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ];

iii. Outer join

A outer join simply extends the results of an EQUIJOIN . while using the EQUI
JOIN, we have seen that if there exists records in one table which do not have
corresponding values in the second, then those rows will not be selected. We can
forcefully select such rows by using OUTER JOIN .
Syntax:- SELECT column_name(s) FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_n

17.

iv. Self join


Another form of join is the SELF JOIN which is a join of a table to itself. For
SELFJOIN, we have the need to open two copies of the same table.
Syntax:- SELECT column_name(s) FROM table1 T1, table1 T2 WHERE condition;

v.Inner join

Select records that have matching values in both tables.


syntax:- SELECT column-names
FROM table-name1 INNER JOIN table-name2
ON column-name1 = column-name2
WHERE condition

18.

LAB QUERY
11. Write a query to find all employees who earn more than the average salary in
their departments.

Output:-

12. Get department name and total no of employees for department.

Output:-

19.

13. Get employee name salary and max salary offered by his department.
Output:-

20.

14. Get total salary provided by sales department.


Output:-

15. Get department name and maximum salary provided by that department. If
there is no employee for department display maximum salary as 0.

Output:-

21.

16. Get the number of employees whose salary is greater than their manager’s salary.
Output:-

17. Get Manager Name and total number of employees working under that
manager.

Output:-

22.

18.Get employee name, salary and average salary of their department.


Output:-

23.

19. Get no of employees department wise, for department with no employee


display count 0.
Output:-

24.

20. Display the names of employees who earn a salary more than that of X and of
Y.

Output:-

25.

S-ar putea să vă placă și