Академический Документы
Профессиональный Документы
Культура Документы
SQL Design:
▪ IBM first developed SQL in 1970s. Also, it is an ANSI/ISO standard.
▪ It is a standard universal language.
▪ It is used by most of the RDMS (Relational Database Management Systems) such as Oracle,
MySQL, MS-SQL, Sybase etc.
Data integrity:
▪ It means which maintains the correct data in the database.
▪ Data integrity: - it means which maintains the correct data in the database
▪ We can preserve data integrity by two types.
1. Data Types
2. Constraints
1. Data types:
Which ensures the type of data which you can store in each column.
Examples: Int, Varchar, Text, Date etc.
2. Constraints.
These are the restrictions or conditions that are used on the col of the table to preserve
data correctness.
There are 7 constraints present in all RDMS
1. NOT NULL
2. UNIQUE
3. PRIMERY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT
7. INDEX
1. NOT NULL: Ensure that a column can not have a null value.
2. UNIQUE: Ensures that all values in a column are different.
3. PRIMERY KEY: A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in
a table.
4. FOREIGN KEY: Uniquely identifies a row/ record in another table.
5. CHECK: Ensures that all values in a column satisfies a specific function.
6. DEFAULT: sets a default value for column when no values specified.
7. INDEX: used to create and retrieve data from the database very quickly.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
1. Employee Table
2. Department Table:
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Statements Description
Select Retrieve data from the table
Insert Insert data into a table
Update Update existing data with new data within a
table
Delete Deletes the records/ rows from the table
Merge Merge( also called UPSERT) statements to
insert new records or update existing records
depending on condition matches or not
Lock table Lock table statements to lock one or more
tables in a specified mode. Table access
denied to a other users for the duration of
table creation.
Call explain plan Statements are supported in PL/SQL only for
executed dynamically. Call a PL/SQL
program or EXPLAIN PATH access the data
path.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Note:
SQL is not case sensitive.
But SQL Data is a case sensitive.
1. Number Datatypes:
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
2. Character Datatypes
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
4. ROWID Datatypes
ROWID data type represent actual storage address of a row. following are ROWID datatypes
in Oracle SQL:
5. Date/Time Datatypes
Variable that has date/time data type hold value call datetimes. Oracle SQL automatically
converts character value in to default date format ('DD-MON-YY') TO_DATE values.
Following are Date/Time data types in Oracle SQL:
TIMESTAMP data type to store valid date (year, month, day) with time (hour,
minute, second).
Type TIMESTAMP Type
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Variable that has interval data type hold value call intervals. Following are Interval data types
in SQL:
Data Type Description
INTERVAL YEAR TO MONTH data type to store and manipulate intervals of year
and month.
INTERVAL
Syntax : INTERVAL YEAR [ (year_precision) ] TO MONTH
YEAR TO
Note : precision specifies number of digits in years field range from 0 to 9 and
MONTH
default is 2.
Size of datatype 5 bytes fixed.
INTERVAL DAY TO SECOND data type to store and manipulate intervals of days,
hours, minutes, and seconds.
INTERVAL Syntax : INTERVAL DAY[(day_precision)] TO
DAY TO SECOND[(fractional_seconds_precision)]
SECOND Note : day_precision specifies number of digits in days field range from 0 to 9. The
default is 2.
fractional_seconds_precision specifies number of digits in days field range from 0
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
to 9. The default is 6.
Size of datatype 11 bytes fixed.
Data
Description Storage(Maximum)
Type
Size: up to 4GB (232 - 1
BFILE data type to store large binary object into Operating bytes)
System file. This data type variable store full file locator's path Directory name: 30
BFILE
which point to a stored binary object with in server. BFILE data character
type read only, you can't modify them. File name: 255
characters
BLOB data type same as BFILE data type to store unstructured Size: 8 TB to 128 TB
BLOB binary object into Operating System file. BLOB type fully (4GB - 1) *
supported transactions are recoverable and replicated. DB_BLOCK_SIZE
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
QUERIES:
1. Display emp no, emp name, sal as monthly salary and annual salary for all the employees.
2. Display ename, sal as old salary and salary increase with 300 as new salary from emp table.
3. Display emp name , his job hired date , monthly salary and annual salary along with commission
of 360.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
4. DISPLAY EMP NAME ,JOB , MONTHLY SALARY WITH QUARTERLY COMMISSION OF 560.
5. Display emp name, monthly salary , monthly commission, annual salary along with commission of
300 given to 2 quadrants .
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
CONCATANATION:
|| is represented by double vertical line and is used to concatenate columns or col with a string
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Where clause:
SQL WHERE clause is basically use for fetching specific criteria matched data only return.
SQL WHERE clause is optionally clause in DML statement.
SQL WHERE clause we can use with SELECT, UPDATE, DELETE statements. SELECT
statement with WHERE clause execute, fetching all table rows and apply WHERE clause
filtering and finally return the result data.
Notes: You can't use INSERT statement with WHERE clause. But you can use INSERT
Statement with WHERE cause only when you get the filter data from another TABLE.
Syntax
Considering following syntax that help you to understanding WHERE clause,
SELECT * FROM table_name WHERE condition;
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Syntax
Considering following syntax that help you to understanding WHERE clause,
UPDATE table_name SET column_name=value1, ... WHERE condition;
Syntax
Considering following syntax that help you to understanding WHERE clause,
DELTE FROM table_name WHERE condition;
Syntax
Considering following syntax that help you to understanding WHERE clause,
INSERT INTO table_name (column_name1, column_name2, ...)
SELECT column_name1, column_name2, ...
FROM another_table_name
WHERE condition;
9. Display the manager no for all the employees who are working in dept no 10.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
12. display employee number , job for all the employees whose manager number is 7698
13. Display all the employees who doesn’t belongs to deptno 20.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
14. Display all the employees who are working in dept. no 20 or job is Manger
15. Display all the employees whose manager number is 7839 or job is analyst
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
16. Display all the employees whose commission is >= 100 and job is salesman
17. Display all the employees whose manager number is 7698 and joined from 20-feb-81
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
18. Display all the employees who doesn’t belongs to deptno 20 and getting commission
from emp table.
19. Display all the employees who doesn’t belongs to deptno 30 and not getting some
commission from the employee table .
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
SQL IN Condition
SQL IN condition used to allow multiple value in a WHERE clause condition. SQL IN
condition you can use when you need to use multiple OR condition.
SQL IN condition allow only specific value in INSERT, UPDATE, DELETE, SELECT
statement.
Syntax
WHERE column_name IN (value1, value2, ...);
Syntax
NOT IN condition use with WHERE clause to exclude defined multiple values from record
data.
WHERE column_name NOT IN (value1, value2, ...);
20. Display all the employees who are working in deptno 10 or 20 and job is manager or
salesman.
21. Display all the employees whose manager number is either 7698 or 7839 or job is
manager or joined from 20-SEP-81
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
22. Display all the employees who doesn’t belongs to deptno 30 and 40 and job is salesman,
clerk or president .
23. Display all the employees who are working in deptno 30 or 60 or 90 and getting some
commission and manager no is 7698 or 7839
NOT OPERATOR:
• If the condition is true it returns false or vice-verse
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
24. Display all the employees who joined before 23-JAN-82 and the mgr is not as 7566, 7839
and salary is <=3000.
25. Display all the employees whose job is president , manager or analyst and salary is >=
300 and working in deptno 10 or 40
26. Display all the employees whose emp no is 7499,7934 and working as clerk or manager
in deptno 10 or 20 with a salary of 1300 or 1600
27. Display all the information from the emp table if the location is DALLAS ,CHICAGO
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
28. Display all the employees who are working in deptno 30 or 60 or 90 and getting some
commission and manager no is 7698 or 7839
29. Display all the employees who joined before 23-JAN-82 and the mgr is not as 7566, 7839
and salary is <=3000.
30. Display all the employees whose job is president , manager or analyst and salary is >=
300 and working in deptno 10 or 40
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
31. Display all the employees whose emp no is 7499,7934 and working as clerk or manager
in deptno 10 or 20 with a salary of 1300 or 1600
32. Display all the employees who are not getting commission from the employees table .
33. Display all the employee whose job is analyst or manager and not working in deptno 30 or
10 from the emp table
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
34. Display all the employees whose manager no is 7566 or 7788 and not getting commission
with designation as analyst or president .
SQL Between clause support only range type value like number, dates, character. But not
supporting boolean, string value range.
Syntax
Considering following syntax that help you to understanding BETWEEN clause,
SELECT * FROM table_name
WHERE column_name
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
When SQL query with BETWEEN clause parse into SQL Buffer, it will automatically expand
out into separate comparison clauses.
SELECT * FROM table_name
WHERE column_name <= lower_bound_value
AND column_name >= upper_bound_value;
35. Display all the employee whose salary is in the range of 800 and 3000. And getting some
commission with manager no 7698 or 7782.
36. Display all the employees whose emp no in the range of 7566 and 7840 with designation
as manager, analyst , salesman
37. Display all the employees whose salary is not in the range of 2500 and 5000 and getting
some commission with deptno 30,40, or 20
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
38. Display all the employees whose manager No not b/w 7699 and 7788 and sal is not in the
range of 1600 and 2500 with comm >=500
Wildcard Description
_ Underscore sign matches any exactly single character with in string.
(underscore sign) Example. 'Op_l Kole', '_pal Kole', 'Opal Kol_'
SQL LIKE Condition used with WHERE clause to matches specific pattern. SQL LIKE
condition apply on table column data.
Syntax
Considering following SQL LIKE condition syntax,
SELECT * FROM table_name
WHERE column_name LIKE 'pattern';
Pattern write inside opening or closing single delimit characters (''). Do not use double delimit
characters ("") because double delimit use as delimiter identifier.
...WHERE column_name LIKE 'pattern'; -- Correct way
...WHERE column_name LIKE "pattern"; -- Incorrect way
40. Display all the employees whose name having char E last char but 1 char from the emp .
41. Display all the employees whose job having substring MAN in it
42. Display all the employees whose job having L as 3rd char and M as last but 2nd char
43. Display all the employees whose name having exactly 5 char
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
44. Display all the employee who are not getting some commission and name doesn’t start with
S and salary is not in the range of 3000 and 5000.
45. Display all the employees whose job having char A and manager no is not as
7902,7788,7782 and salary is in the range of 800 and 3000 .
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
46. Display all the employee Whose salary is in the range 7499 and 7840 and name start with
K as firs char and don’t have manager
47. Display all the employees whose job is manager and who don’t have a manager with
salary >= 3000 from the employee table.
48. Display all the rocords of the tables by sorting the sal in descending order for those who
are working in deptno 20 and 30.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
49. Display all the records of the table and sort employee name in ascending order for those
employees, whose job is salesman, clerk, or analyst.
NOTE:
• We can pass more than one column in the order by clause to sort the data either in
ascending/descending
• If we pass more than one column in the order by clause, always the first priority will
be given to first column, if we have same values or duplicates values in the first
column, then only those two duplicate values adjacent column data will be sorted
based on sorting technique specified.
50. D
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
51. H
NOTE:
• The data can be sorted using number literal only if the order by item(col name
specified in the order by clause ) must be the number of a SELECT list expression
52. H
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
FUNCTIONS
• Are reusable program, which is a very powerful future in SQL.
• In SQL, function may or may not take the inputs but always return the output.
• It is used to perform the following
i. Calculation
ii. Modifying the o/p for each n every record
iii. Modifying the o/p for group of records.
• Functions are classified into
a. Predefined functions
b. User defined functions
I/P1
I/P2 FUNCTION
O/P
I/P3
1.
Oracle SQL provide buit-in SQL functions. SQL Functions take some values as a arguments,
perform some function logic and returning some values. SQL inbuit function are so many that
all are help us for no need to implementing your own logic.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
DUAL TABLE:
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
NESTED FUNCTION:
Syntax :
INITCAP(string)
Example :
Consider following example return the capitalize string of given string argument.
SQL> SELECT INITCAP('hanumanth') "INITCAP" FROM DUAL;
INITCAP
---------
Hanumanth
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Syntax :
LOWER(char/string)
Example :
Consider following example return the lowercase string of given string argument.
SQL> SELECT LOWER('HDB') "LOWER" FROM DUAL;
LOWER
---------
hdb
Syntax :
UPPER(char/string)
Example :
Consider following example return the uppercase string of given string argument.
SQL> SELECT UPPER('hdb') "UPPER" FROM DUAL;
UPPER
---------
HDB
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
53. Display all the employees names in upper case , job in lower case and display the first
character of each in upper case and rest of the character in lower case.
55. Display all the employees whose job having more than 6 characters.
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Syntax :
CONCAT(string1, string2)
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Syntax :
REPLACE(string, match_string, replace_string)
Parameters :
Example :
Consider following example return the every matched string replace with new string.
SQL> SELECT REPLACE('hanumanth d b','h','happa') "REPLACE" FROM DUAL;
REPLACE
-----------
happaanumanthappa d b
Syntax :
TRANSLATE(string, match_char, replace_char)
Parameters :
HANUMANTAPPA D B 9590410403
COMPLETE SQL NOTES
Example :
Consider following example return the every matched character replace with new
character.
SQL> SELECT REPLACE('hanumanth d b ','b','v') "TRANSLATE" FROM DUAL;
TRANSLATE
---------
hanumanth d v
HANUMANTAPPA D B 9590410403