Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

SQL Notes

Notes SQL

Uploaded by

Petrim Annika
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Notes

Notes SQL

Uploaded by

Petrim Annika
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

SQL

SQL, or Structured Query Language, is a programming language used to


talk to databases. With SQL, you can store, manipulate, and retrieve data
from relational databases.

Data

Raw facts that can be processed into the meaningful information

Database

It is a centralized location where we can store large amount of data. There


are number of databases available in the market like My SQL, ORACLE,
Mongo DB, Postgre SQL.

DBMS

A database management system (DBMS) is system software for creating


and managing databases. A DBMS makes it possible for end users to
create, protect, read, update and delete data in a database. DBMS
essentially serves as an interface between databases and users or
application programs, ensuring that data is consistently organized and
remains easily accessible.

user DBMS DB

Objectives

1. Mass storage:
DBMS allows to store thousands of data in the database. We can fetch the
data whenever required.
2. Data Redundancy:
If we are storing the same data multiple times in the database then the data
duplication will occur. Here DBMS guaranties, it won’t accept any
duplication data in the database.
3. Data Protection:
Bank details should always kept secure. Here DBMS provide a master level
security for the data in the database.
4. Platform Independent:
We can run DBMS on any Platforms.
5. Improved Data sharing:
The data stored in the database can be retrieved very quickly, sufficiently,
and securely across any organisation with the help of DBMS.

Types of DBMS
1. HDBMS ( Hierarchial DBMS):

In HDBMS the data will be stored in the form of tree structure

2. NDBMS (Network DBMS)

IN NDBMS, the data will be stored in the form of structure but all the data will
be interconnected.

3. RDBMS ( Relational database)

In RDBMS, the data will be stored in the form of tables, in this case both
inserting and retrieving is easy.

4. Object oriented DBMS

In object oriented DBMS, the data will be store in the form of objectives

Data Models

Data models are used to define how the data is processed and stored into the
database.

Data Types

Data types are used to define what kind of data that can be entered or stored
into the column of the table

Types

 Numeric data type


 String data type
 Date data type
 Boolean data type

1.Numeric data type


The number data type classified into two types

 Number
 Integer
2. string data type
The string data type classified into three types
 char(size)
 varchar(size)
 varchar2

among these three types, we will prefer to use varchar data type because, left
out memory will be wasted. Both Varchar and varchar2 are same but varchar
allows 2000 bytes of memory where as varchar2 allows 4000 bytes of memory

3.Date data type

For date data type, no need to give the size and default date format in SQL

’DD-mm-YY’

SUB Languages in SQL

The sub languages of SQL are fetching the data, creating our own tables in the
database, performing some alterations to the created table and so on.

Types of sub languages

 DQL
 DML
 DDL
 TCL
 DCL

DQL (Data Query Language)

DQL is mainly used to fetch the data from the data base. In DQL we have only
one command that is

 Select

Syntax
Select * from table _ name;

Select column1,column2 ………. column n from table _name;

Select- select command is used to fetch the data from database

From- from clause is used to specify from which table we are going to fetch
the data

*- is an operator which will display all the records from the table

Questions

1. Write a query to fetch all the data from the Emp table
2. Display name and salary of the emp table
3. Display name, designation and their joining date from the employee
table
4. Display the department information of the employee table
5. Display the department name of the employee

Operators

An operator is reserved or character or symbol that performs some


operations.

Types of operators

 Arithmetic
 Logical
 Relational
 String
 Set
 Special Operator

1. Arithmetic Operator

An operator which is used to perform the mathematical operation like


+,-,*, %, / is known as arithmetic operator
Questions
1. Display name of the employees and salary with increment of 500 for all
the employees
2. Write a query to fetch the annual salary for all the employees
3. Display the name and half of their salary
4. List the employee name, commission and salary with deduction of 300
for all the employees
5. Display the name, joining date and salary with the hike of 30%

Where clause – where clause is used to fetch the particular record from
the table

Syntax for where clause

Select column1,column2,.……column from table _name where condition;

1. Display all the details of employee smith


2. Display the name, employee id, commission and department number of
the salesman
3. Display the department name of the employees who is working in new
york location
4. Write a query to fetch the all details of employees who joined the
organization on “1981-04-02”
5. Display the name and department number of employees who is working
in department 20

Literals
A literals is nothing but a fixed data which might be available inside
the table or outside the table and literal are some constant

Literals are mainly used to fetch a specific format of output according to


the user of developer requirement
Types of literals

 String literal- select “hello” from table name


 Number literal- select ‘420’ from table name
 Date literal- select ‘may-20-2005’ from table name
Dual
Dual is a dummy table which is automatically developed by MY
SQL database or ORACLE database.
The main use of dual is to display the output only once and
mainly dual table is used while working with literals
Select “hello” from dual;
It consist of one record and one attribute

Logical operator
All the logical operators will display the output based on multiple
condition checking
Types of logical operators
1. Logical AND
2. Logical OR
3. Logical NOT
1. Logical AND
If both the conditions are true the AND operator gets executed and
display the output
2. OR
If one conditions are true, then OR operator gets executed and
displays the output
3. NOT
It displays the output when none of the conditions are true
Questions

1. list all the salesman in department 30


2. Display the details of employee smith, allen & Scott
3. Write a query to fetch details of employee smith and getting a
salary of 800
4. Write a query to fetch the details of employees whose salary is
not greater than 2500
5. Display the name and salary of either analyst or clerk
6. Display the department information of employees who are
working in research department or who are working in Chicago
7. Display the department name of employee who are not working
in dallas

Relational Operator
The relational operator are used to compare two values or two
expressions it is also known as comparison operator

questions

1. Display details of the employees who are getting salary more


than 2000
2. Write a query to fetch the employee details who are getting
salary atleast 1000
3. Display the name of the employee except all the salesman
4. A query to fetch the details of employees who are earning exactly
3000 as salary
5. Display all the details of employee who are all getting comisson
less than 500
6. Display the name, salary and department number of employees
who all are joined after 1 year ‘31-dec-81’

Special Operator

A special operator are used to perform some specific operation such as

 Like
 Between
 Is null
 In
 AS

Like Operator

Like operator is used to search query specified pattern. Incase like operator we
have to use, two wildcard characters ‘%” & “_”.

When we don’t the number of characters present beside the given


character then we have to use “%”
When we know the number of characters between the given character, then
we have to use “_”.

Syntax

Select column1 ,column2,…….columnn from table_ name where

Column _ name like pattern;

Questions

1. Display the details of employees whose name is starting with “s”


2. Write a query to fetch the employees details whose name ending
with character “S”
3. List the employee name, id and salary of employees whose
designation starts with “S” & end with “N”
4. Display the department name of employees whose working
location consists of “O” as a second character
5. Write a query to fetch designation and salary of employees
whose name consists of “LL”
6. Display the department information of employees whose
department starts with “O” 5th character is “A” and ends with “S”

Between operator

Between Operator is used to select values within the given range.


Between operator is inclusive (starting range and ending range also
included)

Syntax

Select column1,coilumn2,……..columnn from table _ name where


column _ name between low _ range and high range;

QUESTIONS

1. Display the name of employees whose salary is in the range of


1000 t0 3000
2. Display the name and salary whose name is in the range “A” to
“K”
3. Display the department information of employees whose
department range is in the range of 10 to 30
4. Write a query to fetch the employee details whose commission is
in the range of 500 to 1000
Note
For characters, between operator won’t fetch the ending range,
it will fetch the character before the ending range by default.
IS NULL OPERATOR
IS NULL operator will fetch the null values present in the column
of a table

SYNTAX

Select column1,column2,………column from table name where


column_ name is null;

Questions
1. List all the employees details who are all not getting
commission
2. Display the employees who all are earning commission
3. Display the details of employees who don’t have repoting
manager
4. Display the details of employees who all are having reporting
manager

IN operator

In operator is used to fetch multiple data from the same column. In


operator works as short hand of OR operator

Syntax

Select colmn1, column2,……..columnn from table_ name where


column_ name in(values);

Questions
1. Write a query to fetch the details of smith, allen and scott
2. Display the name and salary of employees who are working
department 10 & 20
3. Display the department information of employees who are
working in research and sales department
4. Write a query to fetch employee id, name and commission of
all the salesman and the manager

AS or Aliasing Operator

As operator is used to change the name of the column or


name of the table temporarily

Syntax

Select column_ name as aliasing name from table _ name;


Or
Select column _ name aliasing name from table _ name;
Questions
1. Write a query to change the column name such as ename to
employee name, job to designation, hire date to joining date
2. Display empno, sal and commission of employees with proper
aliasing name

Set operator
Set operator are used to fetch the information of same data
type and same values from two or more tables

conditions
 Number of columns in each result set should be same
 Column data types should be compatible

Types
 Union
 Union all
 Intersect
 Except

Union

It will fetch the unique values by removing the duplicates

Syntax

Select column_ name from table_ name union select column_


name from table _ name

Union all

It will fetch the data including the duplicates

Syntax

Select column_ name from table _ name union all select


column _name from table_ name

Intersect

It will fetch common records from result sets

Syntax

Select column_ name from table_ name intersect select


column _ name from table _ name

Except

It will fetch the unique values in result sets

Syntax

Select column _ name from table_ name except select

column _ name from table – name


Questions

1. List all the salesman in department 30 and having salary


greater than 1500
2. Display the details of employees who all having reporting
manager in department 10
3. List all the employees whose name does not “s” or “A”
4. Write a query to fetch the employee details who are not
getting commission and working as clerk
5. Display the name and salary of employees who do not have
reporting manager department 10 or 30
6. List all the employees names with annual salary who are
working in department 10
7. Write a query to fetch the employee details whose name start
with vowels
8. Display the details of employees who are salesman having “E”
AS the last character in e name and salary consist exactly 4
digits
9. List all the analyst in department 20 and having salary in the
range of 3000 to 5000
10.Display the details of employees who are joined after the year
31-dec-81
11.List the employees details who joined in the month of
February 81

DML (Data manipulation language)

It is used to manipulate the data for temporary, if we need to


make it as permanent, we use commit the operation of DML
to perform, entered into buffered. By using TCL command we
need to store the data permanently.

DML buffer(commit) DB

DML commands

 Insert
 Update
 Delete

Insert

It is used to inserting the record

Syntax

Insert into table_ name values (value1,value2);

Or

Insert into table _ name (column1,column2)values(value1,value2);

Note

If we try to insert low number of columns data also will get error

Update

Update command is used to update or to modify the values in the


table. For Updation where condition is compulsory. At a time we can
update multiple columns

Syntax

Update table_ name set column_ name1=values, column_

name2=values,…… where condition

Questions

1. Update scott salary to 5000


2. Update the designation to hr & commission to 700 of
employees who joined ’22-feb-81’
3. Write a query to update the salary to 2000 and department
number to 20 and designation to hr of miller

Delete

Delete command is used to remove a particular record from the


table.
For deleting particular record where condition is compulsory

Syntax

Delete from table_ name where condition;

Questions

1. Write a query to delete the records of employee smith


2. Write a query to delete the records of employees working in
department no 20
3. Write a query to delete department information of sales
department

constraints

Constraints are nothing but the rules that we are going to


provide while creating a table

Types of constraints

 Unique
 Not null
 Default
 Check
 Primary key
 Foreign key

Unique

It ensures all the values of a columns are unique no


duplication will allow for one table, we can have multiple unique
constraints

Not null

It ensures there will be no null values present in the column of the table
for one table, we can have multiple not null constraint

Default
It set a default value, if no values is specified by a user. For one table ,
we can have multiple default constraints

Check

It will allows the values to enter into the columns if it satisfies a specific
condition. For one table, we can have multiple check constraints

Primary key

It is a combination of both not null and unique. Primary key is mainly


used to identify the each row of the table uniquely. For one table we can
have only one primary key

Foreign key

It is used to connect or join two or more table. The foreign key column
of child table will take the reference of primary key column present in
the parent table and connect both table by using relationship for one
table, we can have multiple foreign key. The foreign key contains both
duplication and not null

DDL (Data definition language)

DDL commands are used to create our own table in the database, to
perform some alterations to the created table and to delete the
unwanted table from the database

Types of DDL commands

 Create
 Alter
 Drop
 Truncate
 Rename

Create

It is used to create table in the database.

Syntax
Create table table_name (coulmn1 data type constraint,
column2 data type constraint,….);

Alter

The alter command are used to add, rename,drop and modify the
column of a table

Alter – ADD

It is used to add a new column to the created table

Syntax

Alter table table _ name add column_ name data type(size)


constraint);

Alter –Rename

It is used to change the name of the column

Syntax

Alter table table-name rename column old_column to


new_column;

Alter –Drop

It is used to remove or delete a particular column from the table.

Syntax

Alter table table_name drop column column_name;

Alter –modify

It is used to change the data type of column at the same time we


can change the size and constraint of a column. Before inserting a
data we need to use modify

Syntax

Alter table table_name modify column_name data type (size)


constraint;
Note

All the DDL commands are permanent if we want to change data


type or size of a column first the column should be empty

Drop

Drop command is used to delete a table from the database

Syntax

Drop table table_name;

Truncate

It is used to delete all the records from the table but it won’t
affect the table. The structure of the table will remain same in the
database

Syntax

Truncate table table_name;

Rename

It is used to change the name of the table

Syntax

Rename table old table_name to new table_name;

Functions

In SQL, we have some predefined functions which perform some


operations

Types of functions

 Single row functions


 Multi row functions

Single row functions


It accepts multiple inputs and generate same number of
output
Types in SRF
 Number
 Char
 date
 Conversion

Number
Number again contains few functions
1 .Mod ()
It will return the remainder of given input
Syntax
Mod (source/input)
Ex:
Select mod (12,10) from dual;
2.abs() (absolute function)
It will convert negative to positive values
Syntax
abs (source/input)
Ex:
Select abs(-15) from dual;

3.Power()

It return the power value of the given input


Syntax
Power(source/value)
Ex:
Select power(2,2) from dual;

4. Sqrt()
It will display the root values of the given input
Syntax
Sqrt(source/input)
Ex:
Select sqrt(16) from dual;
5. Floor()
It used to convert the decimal into integer i.e., from 0.1 to
0.9,it will display the least value as output
Syntax
Floor(source/input)
Ex:
Select floor(13.5) from dual;
6. Ceil()
It is used to convert decimal into integer but from 0.1 to 0.9 it
will display the highest value as the output.
Syntax:
Ceil(source/input)
Ex:
Select Ceil (13.5) from dual;
7. round()
Combination of both floor () and ceil() from 0.1 to o.4 it will
display the least value as output
From 0.5 to 0.9, it will display the highest value as output
Syntax:
Round(source/input)
Ex:
Select round(13.5) from dual;
Select round(13.2) from dual;

Char function:

It is used to deal with character or string.

1.Length()

It display the length of the given string or sentence.

Syntax:

Length (col _name/source)

Ex:
Select length (‘hiiiii’) from dual;

Question

Display the length of all the employee name

3. upper()
It converts lower case to upper case
Syntax:
Upper (col _ name/ source)
Ex:
Select upper ( ‘hiiii’) from dual;
4. Lower ()
It converts upper case to lower case
Syntax:
Lower ( col _ name /source)
Ex:
Select Lower ( ‘SMITH’) from dual;
5. Substr()
It is used to fetch the part of the given string.
Syntax:
Substr (input, starting point)
Substr(input, starting point, length)
Ex:
Select substr( ‘Rahul’, 3) from dual;
Select Substr ( ‘Rahul’, 3,5) from dual;
6. Replace()
It is used to replace the given string with another new string
or we can replace a particular character with another new
character.
Syntax:
Replace (input, old char/ str, new char/ str)
Ex:
Select Replace ( ‘ java’, ‘j’, ‘m’) from dual;
7. Reverse ()
It display reversed string as output.
Syntax:
Reverse (source / input)
Ex:
Select reverse (‘java’) from dual;
8. Initcap()
It is used to display a string character of each word into upper
Syntax:
Initcap (source/input)
Ex:
Select initcap ( ‘developer’) from dual;
9. Instr ()
It display the occurrence position of a specified character in
the given string.
Syntax:
Instr ( input, char)
Ex:
Select instr( ‘developer’, ‘v’) from dual;
10.Ascii ()
It return the Ascii value of a specified character or string.
Syntax:
Ascii( source/input)
Ex:
Select ascii(“developer”) from dual;

Questions
1. Write a query to display the details of odd salaried from
the employee table
2. To display the details of even salaries
3. Display the details of employees whose name is consist of
exactly four characters
4. Display the details of employees whose name is consist of
exactly seven characters
5. Display first three character of employee name
6. Display last three character of employee designation
7. Write a query to fetch the details of employee whose job is
having “man” as first three character
8. Display the output of “development” from the input
“developer”
9. Display the last 5 character of department name in lower
case

Date function
1. Current_date
It displays the today date
Ex
Select current_date() from dual;
2. Sysdate()
It displays current system date with time
Ex
Select sysdate() from dual;
3. Month()
It displays today month
Ex:
Select month(“2023-08-08”) from dual;

Multi row function


All the multi row function accept multiple input and
generate only one value as the output
Types
 Max
 Min
 Sum
 Count
 Average
 Trim

Max()

It will return the highest value among the given input

Question

1. Display the highest salary among all the employees


2. Display the highest salary among the all salesman
3. Display all the last joining of the employee

Min()

1. It will return the least value among the given input

Questions

1. Display the minimum salary of all employees


2. Display the least salary of employee working in department
20
3. Display the first joining of all employees

Sum()
It displays the sum of the given value

Questions

1. Write a query to fetch the sum of sal of all the employees


2. Display the sum of comm of employee working in dept 30

Average()
It will return the average value of the given input

Questions
1. Display the average salary of employee working in 10

Count()

It is the function which is used to count the number of rows


present in the column of the table

Questions

1. Display the number of employee earning commission in the


employee table

Trim()
It is used to remove the starting and ending spaces of the
given input
EX:
Select trim(“ name “) from emp;

Sub Queries

A query inside another query is known as sub query


Syntax
Select col_name from table_name< where condition>
(select col_name from table_name);

Use case of sub query


1. If we want to fetch the output from one table based on
input of another table

Execution process

1. Inner query will execute


2. Based on the inner query output the outer query
execute

Types of sub query

1. Single row sub query


2. Multi row sub query
3. Co-related sub query
1. Single row sub query
If the inner query is displaying only one row output then
it is known as single row sub query
Incase of single row sub query we can use equal
operator between inner query and outer query

2. Multi row sub query


If the inner query return more than one output then it is
known as multi row sub query
Incase of multi row sub query we can use ‘IN’ operator
between inner query and outer query
Questions
1. Display the details of the employees who is used to
getting highest salary
2. Display the details of the employees who is used to
getting lowest salary
3. Display the department information of employee
smith
4. Display the details of employee whose working in
research and sales
5. Display the details of employees whose designation
same jones designation
6. A query to fetch name and designation of employee
whose same as martin
7. Display the department name of the employee
whose designation ends with “N”
8. Display the details of employee whose salary is
greater than miller
9. Display the details of smith manager
10.Display the name, salary of the employee who are
not getting commission
11.Display the details of department who don’t have a
reporting manager
12.Display the second highest salary
13.Display the third highest salary
14.List all the employee whose salary is greater than
average salary of department 20
15.Select all the employee who are all working in dallas
16.Display the details of smith’s managers manager
17.Display the department name of employee who is
getting highest commission
18.Display location of blakes managers manager
19.Display the name and designation of employee who
is getting max salary of accounting department
20.Display the number of employee has the same
number of location as adams manager
21.Display the department name of employee whose
name does not start with “s” and salary lies in the
range of 1500 t0 3000

22.Display the location of employee who is getting min sal

Order by

Order by clause is used to sort the data of the column either in


ascending or descending order

By default order by clause will sort the data in ascending order

Syntax

Select col_name from IN where condition order by col_name asc/desc;

1. Display the employee name, salary and designation of employee where


the employee name is sorting in ascending order
2. Display the name of the employee order by designation in descending
order
3. Display the details of employee and sort the employee

Group by clause

1. It is used to group the row with same value


2. It is use foe single table
3. In this select statement of group by clause we can select the multi row
function along with their column which we are going to group

Syntax
Select col1, col2, from employee group by col_name;

Questions
1. Display the highest salary in each designation
2. Write a query to fetch the highest salary in each department
3. Display the total salary given to each designation
4. Display the no. of employee working in each department
5. Display the no. of employee working in each department except
department 20

Having clause

It is added to SQL to provide the condition for multi row function

Syntax

Select cn1,cn2……

from table_name

Where condition

Group by column_name

Having condition;

Question

1. Display the highest salary in each designation only if highest salary is >
2000
2. A query to fetch the salaries that are repeated
3. Display the count of employee in each department except department
20 and count should be less than 6
4. Display the average salary of each department only if the average salary
is > 3000

Transaction controlling language (TCL)


All the TCL commands are used to manage the changes, that are made
by DML commands
 Commit
 Save point
 Rollback

Commit
it is used to save the data permanently in the database
syntax
commit;

save point
it is used to store the data temporarily under a particular save
point
syntax
save point save point_name;
example
save point A;
insert into writer values (1, “raj”,22);
save point B
insert into writer values (3, “ram”, 22);

Rollback
It is used to restore the data upto last committed state . we can do
rollback only when the data is store permanently
Syntax
Rollback to save point save point_name;
Ex
Roll back to save point A;

Normalization
It is used to organize the data in the database
It is help to reduce the data redundancy
It is also used to eliminate insetion,updation and deletion
anomalous

Types of anomalous normalization


1. Normal form
2. Second normal form (2 nf)
3. Third normal form (3 NF)
4. Boyce Codd normal form (BCNF)
First normal form (1 NF)
If a table wants to be a 1 NF it should satisfy the following
conditions
1. All the column name should be unique
2. The domain of the table should be same
3. The table should contains only single value attribute

Second Normal Form (2NF)

If a table wants to be 2NF we should satisfy the following


conditions:

1. It should satisfy all the condition of 1NF


2. The table should not contains any functional dependency
among the attribute.

Functional / partially dependency is nothing but all the non-


prime attribute should depend on prime attribute.

Third Normal Form (3NF)

A table is in third normal form if and only if:

1. It satisfy all the condition of 2NF


2. That should not be only Transique dependency among
attributes.
An indirect dependency is known as transique dependency

Boyce Codd Normal Form (BCNF)

BCNF was proposed by Raymond Boyce and E.F. Codd. So it

Was named as BCNF.

 It is an advanced version of 3NF


 BCNF also known as 3.5NF
 The anamolous or problems can able to solve in the first
3 normal forms will be solved in BCNF.

Distinct
It is keyword which is used to display the unique value by
removing the duplicate from the column of the table

Syntax

Select distinct col1,col2,….. from table name;

Ex

Select distinct job from emp;

Limit

The SQL limit clause is used to specify the maximum


number of rows that should be returned from a query

Select distinct salary from emp

Order by salary desc

Limit 1 offset 4;

Joins

The process of retrieving data from multiple tables is


known as joins

Combines data from one or more tables

Types of joins

1. Cross join
2. Inner join
3. Equi join
4. Self join
5. outer join

Cross join( valid & invalid data)


It is also known as cartician join which is implemented from set theory of
mathematician

It is also used to fetch the required data from multiple tables

In cross join each value of the first column all the values of the second column

Cross join can display the output from multiple table without connections

Syntax

Select TN1. CN, TN2.CN…..

From TN1 cross join TN2

Where condition

Questions

1. Write a query to fetch the employee name and department


name for all the employees
2. Select employee name and department name of employee
smith
3. A query to fetch the employee name and half of their
salary and location who are working as a manager
4. Display all the details of employee along with the
department and location
5. Display employee name, join date, annual salary along with
their department name and all the salesman and analyst
with aliasing name

Equi join
Equi join remove the ivalid combination of output which is
produced by cross join
It is same as cross join but the joining condition is specified
In case of equii we need to use (=) operator the main use
of equi join is to remove the invalid combination of data

Syntax
Select TN1.CN,TN2.CN,……
From TN1,TN2
Where condition and
Joining condition;

Questions
1. Display the employee name and department name of all
the employee
2. Display the employee name and department and annual
salary of employee smith who is working in department 20
3. A query to fetch employee name, half of their salary along
with their department information of all employees
4. A query to fetch employee details and location as who is
working as a manager

Inner join
It will fetch the matching rows from each table
Syntax
Select TN1.CN,TN2.CN,….
From TN1 inner join TN2
On condition and
Joining condition;

Questions
1. Display the details of employee along with their
department information
2. Display all the details employee along with their
department name, location who has hired in 18-feb-81
with salary range between 1500-3000

Self join
A table joining itself is known as self join
Syntax
Select aliasing TN1.CN,aliasing TN2.CN
From T1 aliasing name, t2 aliasing name
Where condition and
Joining condition

Question
1. Write a query to fetch name and salary of employee
whose salary is same as martin salary
2. Display the employee name, joining date along with
salary with 30% hike whose department is scott
department
3. Display the employee name, salary along with their
manger name along with their salary
4. Display the employee name, department name , salary
along with their manger name, department number,
salary

Outer join

It will fetch all the data of one table and only the matching
record of another table

1. Left outer join


It will fetch all the data from left side table and matching records from
right side table
Syntax
Select TN1.CN, TN2.CN
From TN1 left join TN2
On condition and
Joining condition

2. Right outer join

It will fetch all the data from right side table and matching records from
left side table

Syntax
Select TN1.CN, TN2.CN
From TN1 right join TN2
On condition and
Joining condition

Full outer join


It will fetch both matching and un matching tables
Combination of both left and right outer joins
Syntax
Select TN1.CN,TN2.CN
From T1 left join T2
On condition and
Joining condition
Union all
Select TN1.CN,TN2.CN
From T1 right join T2
On condition and
Joining condition

INDEX
Index is used to display the output in a faster way
Syntax:
Create index Index _name on table _ name (column1, column2);
Show index from table _ name;
Drop index index _ name on table _ name;
Ex:
Create index test on writer (name);
Select name from writer;
Drop index test on writer;

View
View is the virtual table, it is called as resultant set of store query.
View are mainly used to display only the necessary data to the user by
hiding unnecessary data to the user.
Syntax:
Create view view _name as
Select column1, column2,
From table _ name
Where condition;
Ex:
Create view dummy as
Select name from writer;
Drop view dummy;
Note:
After created view, if we update anything in the existing table, it will
reflect into view table
Advantages:
 It provides security for the data
 It increases the databases performance.

DCL (Data Controlling Language)

The DCL commands are used to deal with rights and permissions of the

Data in the database.

DCL Commands

 Grant
 Revoke
1. Grant
It is used to give permission to the user
2. Revoke
It is used to get back the given permission from the user.
Syntax:
User list
Select user from my sql. user;
Current users
Select users;
Create new users
Create user ‘new-username’@ ‘local host’ identified by ‘password’;
Grant
Grant select , insert, …. ON database-name . table-name to ‘username’
@ ‘hostname’;
Grant all privileges on database-name * to ‘username’ @ ‘hostname’;
Revoke syntax:
Revoke select, insert, …. ON database -name. table -name to
‘username’ @ ‘hostname’;
Revoke all privileges on database-name * to ‘username’ @ ‘hostname’;
Ex:
Use scott _schema;
Select * from writer;
Select user from my sql . user;
Select user ();
Create user ‘raj’ @ ‘local host’ identified by ‘raj’;
Create user ‘raja’ @ ‘local host’ identified by ‘raja’;
Select user from my sql . user;
Grant select , insert on scott _ schema . wrier to ‘raj’@ ‘ local host’;
Select current _ user ();
Update writer set name = “ram” where id=1;
Select sql-safe-updates -0;
Show grants for ‘raj’ @ ‘local host’;
Revoke select , insert on scott _ schema . writer from ‘raj’ @ ‘local host’;
Drop user ‘raj’ @ ‘local host’ ;

Grant

You might also like