SQLfinal notes- all
SQLfinal notes- all
Processor
-to store the data we need to use database
-to communicate with database, we need a manager.
-that manager is DBMS (database management system )
- to communicate with DBMS we need a language, that language is
SQL.
- by using SQL language ,we write queries.
- with the help of query, we can request the dbms to search for the
data.
- once the query is submitted to dbms, it will start processing the
query.
- the dbms will go inside database and it will search of the data inside
database, if the data we are searching is it present inside database,
dbms will take that data and display that data as output for the user.
Note :
SQL – structured query language . perilously called structured
English query language.
Day 2
What is DBMS :-
-Is a program that stores retrieves and modifies data in the database
on request.
- study of different techniques of design, development and
maintenance of the database.
-One of the job of dbms is only storing the data but also arranging the
data.
-To arrange the data we are having different types of dbms.
1.HDBMS
2. NDBMS
3.RDBMS
4.OBJECT ORIENTED DBMS
2 3
4 5 6
Day 3
After HDBMS
-to rectify the disadvantages in hdbms they came up with another
dbms as Ndbms.
Day 4
Advantages Disadvantages
All the disadvantages in hdbms It become complex with large about of
are rectified data
(when the no of users are increasing
the complexity of the database will be
increased)
entity
Attributes
SQL summary
there are five types of statements used in SQL language/ subsets of
SQL
Emp table
EMPNO ENAME JOB MGR HIREDATE SAL COMN DEPTNO
Dept table
Query example
select * from dept;
Select * from Emp;
(this will display all the data in both the tables)
Questions
Display any one column from emp table?
Ans – select column_ from table_name;
Exp- select empno from emp;
Display any two-column from emp table?
Ans – select empno, ename from emp;
ALIASES
Questions
SQL Literals: -
A literal is an explicit numeric, character, string, or Boolean
value not represented by an identifier. For example, TRUE,
786, NULL, 'tutorialspoint' are all literals of type Boolean,
number, or string. PL/SQL, literals are case-sensitive.
It is a data, we are having four types some of them are,
number literal -50, character/string/ text literal- ‘hello’ ,date
literal- ‘dd-mm’yy’, integer .
If literal is used in select statement, same literal we will
displayed for each and every row, present in the table.
Syntax -
Select 10 from emp; - example for number
Select ‘hi’ from dept; - example for character
Concatenation operator ( || )
The CONCAT() function adds two or more strings together.
Note: See also Concat with the + operator and CONCAT_WS().
Assignment-
1.Write a query to display name of the employee.
-select ename from emp;
2. Write a query to display ename and salary of all employee.
-select ename, sal from emp;
3. Write a query to display department name and its location for
all the department.
-select dname, loc from dept;
4. Write a query to display name, salary, commission and date
of joining of every employee.
-select ename, sal, comm, hiredate from emp;
5. Write a query to display employee name, his designation and
his manager’s employee number.
-select ename, job, mgr from emp;
6. Write a query to display employee name and his department
number.
-select ename, deptno from emp;
7. write a query to display all the details of each and every
employee of the company.
-select * from emp;
8. Write a query to display all the details of department present
in company.
-select * from dept;
9. Write query to display employee name his employee
number, his salary and also his annual salary for every
employee.
-select ename, empno, sal, sal*12 as “annual salary “from emp;
10. write query to display employee name, for employee
number and salary with the hike of 30% for every employee.
- select ename, empno, sal+ sal*30/100 from emp;
Questions: -
1. Display all the information of Scott?
Select * from emp where ename =’SCOTT’;
Distinct: -
Distinct is used to display unique data present inside the
table.
A A
B B
A Deleted
C C
B Delete
D D
E E
D Delete
F F
Example –
Select distinct(job) from emp;
Output: -
JOB
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST
Questions
1.Display employee information whose comm is 1400.
Select * from emp where comm=1400;
Between-one operator: -
When we have any rang of values we can use between- and
operator.
Syntax- column name between lower range and higher
ranger.
Example- select * from emp where sal between 1000 and
2000;
In operator: -
If we have multiple OR condition for a signal column then we
can use IN operator.
Syntax: - column name in (data, ….)
Example – select * from emp where job in (‘MANAGER’,
‘ANALYST’);
Is null operator: -
This operator is use to get all null values in the column.
Is null operator is used to find out the null data that is present
inside the table.
Syntax: - column name is null;
Example: - select * from emp where comm is null;
Not operator: -
Not operator cannot be used solo, it needs to club with any
other operator. Like Not between and, not in, is not null, Not
like.
Example: -
Select * from emp where comm is not null;
Like operator: -
The SQL Server LIKE is a logical operator that determines if a character string
matches a specified pattern. A pattern may include regular characters and
wildcard characters. The LIKE operator is used in the WHERE clause of the
SELECT, UPDATE, and DELETE statements to filter rows based on pattern
matching.
Questions: -
14. Write Query to display all the employee who don’t take
comm.
select * from emp where comm is null;
16. Write a Query to select the name which begins with ‘A’
and ends with ‘A’.
Select ename from emp where ename like ‘A%A’;
18. Write a query to select the name which starts with ‘M’.
Select ename from emp where ename like ‘M%’;
19. Write a Query to match name to select string which is
having substring MAD.
Select ename from emp where ename like ‘%MAD%’;
Functions: -
Functions. "A procedures or function is a group or set of SQL and PL/SQL statements
that perform a specific task.". The major difference between a procedure and
a function is, a function must always return a value,
Initcap: -
This function actually used to get first letter of word uppercase
and remaining in lowercase.
Initcap- inticap(data)
Select ename, inticap(ename) from emp;
SMITH- Smith
MILLER- Miller
Concat: -
Concat function is used to combine two strings. we can only
use two string not more than that.
Concat (ip1, ip2)
Concat (smith, clerk)- smithcleak
Concat (miller, Allen)- millerallen
Syntax- select ename, job, contact (ename, job) from emp;
Nested functions: -
If function is written inside another function it is known as
nested function.
Fun(fun(data, data),data)
Example: -Select length(lower(ename)) from emp;
Replace: -
replace(‘java’,’a’,’b’)-jbvb
replace(‘java’,’x’,’y’)- java
replace(‘java’,’a’)- java
Write a query to display all the employee’s whose name is having ‘R’
as the second last character.
List all the employee whose name is having letter ‘R’ in the 3rd
position.
SUBSTRING FUNCTION
Syntax; substr(ip1,ip2,ip3)
Ip1- data
Ip3 – no of character
Substr(‘developer’,1,3) – dev
Substr(‘developer’,3,3) – vel
Substr(‘developer’,5,1) – l
Substr(‘developer’,-3,3) – per
Substr(‘developer’,5) – loper
Substr(‘developer’,-2)- er
D(1,-9) E(2,-8) V(3,-7) E(4,-6) L(5,-5) O(6,-4) P(7,-3) E(8,-2) R(9,-1)
Question
Display employee names and jobs whose job is starting with man.
Display all the employee details who joined in the month of feb.
Display employee names whose name is having last but one character
as E.
Instring function: -
Ip1 – data
Ip4- occurrence
NVL function: -
It is used to replace null data with zero. For example, we have null
values in comm column in emp table. So, we can use NVL to replace
values with o.
Nvl2 function: -
sysdate: -
Systimestamp: -
Trunc function: -
If you give decimal value as input it will remove decimal and give
integer value as output.
trunc(75.8)- 75
Round function: -
If you give decimal value as input ,it will round off decimal value and
give integer value as output.
round(72.3) -72
round(75.6) -76
round(73.5) -74
round(79.8)- 80
Mod function: -
Example: -
Mod(20,2) – 0
Mod(25,2) – 1
Question:
2. Display the employee names who had worked more than 12000
days.
Select ename from emp where (sysdate- hiredate) >12000;
3. Display employee names, sal, yearsvof experience for all the all
the employees.
Select ename, sal, round(((sysdate-hiredate) /365)) as “years of
experience” from emp;
Order by statement: -
Questions: -
List all the employees who does not belong to department no 20.
Select sum(sal) from emp; (to display sum salaries of all the
employees)
Questions: -
1. Display no of analysts.
Group by: -
Equi join-
Questions: -
Display employee name and department name for all the employee.
Display employee name, location and department number for all the
employees.
For joining of two tables , if we doesn’t use equal to(=) operator then
it’s called as Non equi join.
Syntax: -
Select * from emp, salgrade where sal between losal and hisal;
Cross join: -
While joining two table if we don’t give any join condition then it
performs cross join.
Cross join actually means all the records of first table will be merged
with first record of two table similarly the merging continues till the
end of all the records.
Inner join: -
Inner join will display common information that is present both the
tables.
Outer join: -
Left join: -
The LEFT JOIN keyword returns all records from the left table (table1), and
the matched records from the right table (table2). The result is NULL from
the right side, if there is no match.
Right join: -
The RIGHT JOIN keyword returns all records from the right table (table2),
and the matched records from the left table (table1). The result is NULL from
the left side, when there is no match
Note: FULL OUTER JOIN can potentially return very large result-sets!
Constraints: -
Unique constraint: -
Unique constraint will not allow duplicate data but will allow null data.
For example: - below table we should have unique data in Student Id buut it
will accept null value.
1 Raja 123
2 Rani 234
3 Sweety 124
Fruity 121
Not Null: - Not null constraint will not allow null data but it allow duplicate
data.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key
can consist of single or multiple columns (fields).
A FOREIGN KEY is a field (or collection of fields) in one table that refers to
the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.
Creating table
Syntax:
Child table
Alter: -
Add column-
Rename column: -
Table altered.
Remove column: -
Syntax: -
Table altered.
Table altered.
Renaming table: -
Table renamed.
Drop: -
Table dropped.
Truncate: -
It is used to delete only data inside the table but table structure will
remain as it is.
2. update: -
1 row updated.
3. Delete: -
1 row deleted.
DCL: - DCL is short name of Data Control Language which includes commands
such as GRANT and mostly concerned with rights, permissions and other controls of
the database system.
Syntax:
DTL: -
Syntax: -
Commit – commit;