SQL Jspider
SQL Jspider
JSPIDERS
Rajajinagar
Developed By ALIV HASAN (aliv.hasan@hotmail.com)
What is Database?
Ans. A systematic collection of data .Data in the database is organized and it makes easy to manage the data.
Advantage
Redundancy is eliminated.
Disadvantage
• Searching a data is extremely difficult.
• Searching a particular data needs more time.
RDBMS:
It maintains data in table which are created and maintained across and among the data and table.
Ex, Oracle, Sybase, DB2, Teradata, SQL Server, MySQL etc.
SQL MySQL SQL Server
Grid Computing:
It uses the researches of a many separate computers connected to a network (usually internet ) to solve large scale
computation problems.
Constraints:
Constraints are restriction or conditions that are used on the columns of the table to preserve the data
correctness.
A constraint is a condition which restricts the invalid data in the table.
A constraint can be provided for the column of a table.
Type of Constraint:
1. NOT NULL.
2. UNIQUE.
3. PRIMARY KEY.
4. FOREIGN KEY.
5. CHECK.
1) NOT NULL:
• Ensures that at least some values should be present for attribute or columns.
• In a table we can have more than one not null.
• Null is nothing, it is neither zero nor blank.
• Null will not occupy any space in memory.
• Two null are never some in oracle.
• Null is used to represent the empty values.
2. UNIQUE:
• It is used to have only unique values inside a column.
• Unique constraint doesn't allow you to enter duplicate values into some column
3. PRIMARY KEY:
• It is used to identify each record uniquely.
• It is combination of Not Null and Unique.
• Only one Primary key is allowed in table.
4. FOREIGN KEY:
• It is a referential integrity constraint which creates relationship between tables.
• It is created in child table.
• FK column can have both duplicate and null values
• Masters table should have a primary key defined a column in the master table to create FK.
• We can have more than 1 FK in table
5. CHECK:
• Is used for enforcing some additional condition with respect to business requirements.
• Is used to provide additional validation as per the customers' requirements. Ex, sal>0 , impress should start with
1.
SQL STATEMENTS:
Data Manipulation Language (DML)
1) SELECT
2) INSERT
3) UPDATE
4) DELETE
5) MERGE (NOT COVERED)
Interview Question
Q. How to see all the tables present inside the schema?
Ans. select * from tab;
Q. How do you get to know the column of the table without executing select statement?
Ans. By using desc <table Name>;
2) INSERT:
Is used to add new rows of data to a table.
Syntax,
Insert into table_name values (values1, Val 2,val3…Val n);
3) UPDATE:
Is used to modify the existing rows in a table.
Syntax,
Update table_name
Set column_name1=value,
Column_name2=value, …..
Where condition
Ex,
Update emp
4) DELETE:
Is used to delete rows from a table.
Syntax,
Delete
from table_name
where condition
Ex,
Delete
from emp
where empno=7769:
Note:
TRUNCATE st is also delete all the rows from a table and but free space containing the table
Syntax
Ex,
But the Difference delete and truncate is delete st is delete the rows from the table and does not free the space
containing the table.
If u drop a table , all the rows, table, and structure Is removed from the database.
Syntax, drop table table_name;
Ex,
(id number(5) primary key, name varchar(20) not null, dept varchar(10) not null, exp_date date not null,sal
number(10) not null);
2) ALTER: Is used to make the alter issues for structure of the table. Alter issues can be adding new column drop
the column and rename in column.
Add, drop, modify table column.
Ex,
alter table temp_1
add deptno number (5) not null;
Ex,
alter table temp_1
drop deptno;
Ex,
4) TRUNCATE: is also delete all the rows from a table and free space containing the table
Syntax
Ex,
Commit: Is used to save the DML changes permanently from the database.
Rollback statements written after commit st will not have effect any on the database.
Commit st written after the rollback will not have any effect on the database.
Save Point: is used to rollback the changes to the user define level.
Data Control Language
1) GRANT: Is provide to permission on a table belongs to one user.
Ex, grant
SQL Function:
• Function are a very important feature of SQL and can be used to do following :-
• Perform calculation on data.
• Modify individual data items.
• Manipulate output for group of rows.
• Format dates and Number for display.
• Convert data types.
Dual Table:
Dual table is a dummy table which is used to perform independent calculation.
Dual table contains one single row and one single column.
For Lower:
Q. List all the employ names in lower case?
Ans. Select lower(ename) from emp;
For Upper:
Q. Convert all the employ name to Upper case?
Ans. Select upper(ename) from emp;
For Initcap:
A. List all the employ names in Camel case convertion ?
Ans. Select initcap(ename) from emp;
a. Concat function.
b. Concatenation Operator.
c. LENGTH.
d. REPLACE.
e. Trim.
f. Substr.
g. INSTR.
b. Concatenation Operator:
Is used to concatenate more than two strings.
Exam, >select 'Hi '|| ename ||' Your salary is '||sal
From emp;
c. LENGTH: Is used total lenght of the string.
Q. whose name contains exactly 6 characters?
An. select* from emp where length(ename)=6;
R. Atleast 5 char?
Ans. select* from emp where length(ename)>=5
Q.(1,2) (3,2)
Ans. select instr('Hello World','l',1,2) from dual;
>select instr('Hello World','l',3,2) from dual;
More Exam,
select instr('Hello World','l',-1,2) from dual;
>select instr('Hello World','l',-1,-2) from dual;
ERROR at line 2:
ORA-01428: argument '-2' is out of range
General Function:
NVL Function: Is used to replace the null values within the column.
CONVERTION FUNCTION:
1. TO_CHAR FUNC:
to_char(Date,'format_model')
Comments:
YYYY-Full year in number
YEAR- Year spelled out in English
MM-tow digit value for month
MONTH-full name of the month
MON=three later abbreviation of the month
DY- three later abbreviation of the week
DAY-full name of the week.
DD-numeric day of the month.
Ex, SQL> select to_char (sysdate,'dd/mm/yyyy')
2 from dual;
Ans.
1 select*
2 from emp
2 from emp
3 where to_char(hiredate,'YYYY')=1981;
2 from emp
3 where to_char(hiredate,'DY')='SUN';
HH24:MI:SS AM-15:45:15 PM
Ans. SELECT JOB, SAL AS "OLD SAL", DECODE(JOB,'MANAGER',SAL+SAL*0.1, 'SALESMAN',SAL+SAL*0.05, SAL)AS "NEW
SAL" FROM EMP;
NUMBER FUNCTION:
DATE FUNCTION:
Date: Returns both date and times but the select statements prints only current date and skipped the type.
SQL> select sysdate from dual;
Time: systimestamp is returns 3 things date time with mini sec information time zone.
1. GROUP FUNCTION:
Type of Group Function:
1. AVG
2. COUNT
3. MAX
4. MIN
5. SUM
from emp;
2 from emp;
5. SUM: Is used to find the sumation of all the values within the column.
Q. sum of all the sal of emp table?
2. GROUP BY FUNCTION:
from emp
group by job;
2 from emp
3 group by deptno;
2 from emp
3 group by job;
Interview Q.
1. What is diff group by clause and order by clause?
2 from emp
3 where count(*)>4;
ERROR at line 3:
Correct
from emp
group by deptno
having count(*)>4
Q. display job ways highest sal only the sal is greater than three thousands?
from emp
group by job
having max(sal)>3000;
from emp
group by job
having job!='MANAGER';
3. NESTING FUNC:
-SINGLE ROW FUNCTIONS CAN BE NESTED TO ANY LEVEL.
-NESTED FUNCTIONS ARE EVALUATED FROM DEEPEST LEVEL TO THE LEAST DEEP LEVEL.
F3(F2(....................,ARG2),ARG3)
step2=result2(inner func)
step3=result3(outer)
1. INNERMOST
2. OUTER
3. OUTER MOST
Allen, Blacke,Ward,Adams?
AlleN, WarD,AdamS
from emp;
Q. To replace all with * the letter L in all the positions of a emp table?
from emp:
SUBQUIRES:
Sub query are written to find the unknown values.
Always write the sub with in the parenthesis and right hand side of comparison operator
Used appropriate condition operators for the queries is written single row or multiple rows.
Q. List all the emp who’s sal is less then ALLEN's salary?
Ans. 1 select *
2 from emp
Ans. 1 select *
2 from emp
select * from emp where mgr=(select empno from emp where ename='KING');
Q. List all the employs who job is same as Miller or salary is greater than Allen's sal?
Ans, select *
2 from emp
Ans, 1 select *
2 from emp
3* where sal =(select max (sal) from emp where mgr=(select empno from emp where ename='KING'))
Ans, 1 select *
2 from emp
Ans.1 select *
2 from emp
Q. All the employs who earned sal between 1000-3000 in SALES and ACCOUNT deptmnt?
Ans. SQL> SELECT * FROM EMP where sal between 1000 and 3000
2 and deptno in (SELECT DEPTNO FROM DEPT WHERE DNAME IN ('SALES', 'ACCOUNTING'));
2 from emp where sal<(select max(sal) from emp where sal<(select max(sal) from emp))
SQL JOINS
Joins are used to fetch the data two or more table. In general table are related to each other using FK constraint.
3. Outer join.
4. Self-join
1. CERTESIAN JOIN:
Produces the Cartesian product of the 2 table involved join.
Here record in left table is join with every record right table.
2 from emp,dept;
Returns only the matching row between both the tables and non-matching records are eliminated.
3* ON EMP.DEPTNO=DEPT.DEPTNO
WHERE EMP.DEPTNO=DEPT.DEPTNO
3. OUTER JOIN:
Are used to retrieve matching and non matching rows from wheather left table or right table or from both the tables.
a) Left outer join: It return both matching and non matching records from left table.
ON EMP.DEPTNO=DEPT.DEPTNO;
b) Right outer join: Written both match it and none match it records right tables.
3* ON EMP.DEPTNO=DEPT.DEPTNO;
c) Full Outer join.Is writen both matching and non matching records into left and right table
3* ON EMP.DEPTNO=DEPT.DEPTNO
4. Self-join:
Join in the table to itself called self-join.
3* where a.mgr=b.empno
CO RELETED SUBQUIRY:
Here the output of inner query and outer query are inter dependent
Q. max sal?
Ans. 1 select *
2 from emp a
Ans. 1 select *
2 from emp a
Ans.
1 select *
2 from emp a
NORMALIZATION:
Normalization is process of efficiently organizing the data in the database.
We do normalization
1. Eliminating redundant data. (Storing same data in more than one table)
2. Ensuring data dependency makes sense. (Ensuring only related data is stored in the table)
First Normalization:
2nd Normalization:
3rd Normalization:
Denormalization:
CODD RULES:
Any DBMS to qualify to become an RDBMS should be following rules:
2. DBMS should support constraint like unique, primary key, foreign key, and check.
4. The DBMS should support quarry technic like, sub query, joints, and co-related sub query etc.
THE END
***ALL THE BEST FOR THE MOCK INTERVIEWS.