Beruflich Dokumente
Kultur Dokumente
SQL Operators
OR" Logical Operator:
--> SELECT first_name, last_name, subject FROM
student_details WHERE subject = 'Maths' OR subject ='Science'
be like
--> RENAME employee TO my_emloyee;
SQL Joins
The Syntax for joining two tables is:
SQL Views
The Syntax to create a sql view is
CREATE VIEW view_name
AS SELECT column_list FROM table_name [WHERE condition];
For Example: to create a view on the product table the sql query would be like
CREATE VIEW view_product
AS SELECT product_id, product_name FROM product;
SQL Subquery
For Example:
--> SELECT first_name, last_name, subject
FROM student_details WHERE games NOT IN ('Cricket',
'Football');
- - > SELECT id, first_name FROM student_details
WHERE first_name IN ('Rahul', 'Stephen');
--> SELECT id, first_name
FROM student_details
WHERE first_name IN (SELECT first_name
FROM student_details WHERE subject= 'Science');
Correlated Subquery
--> SELECT p.product_name FROM product p
WHERE p.product_id = (SELECT o.product_id FROM order_items o
WHERE o.product_id = p.product_id);
SQL Index
Syntax to create Index:
CREATE INDEX index_name ON table_name
(column_name1,column_name2...);
Syntax to create SQL unique Index:
CREATE UNIQUE INDEX index_name ON table_name
(column_name1,column_name2...);
Creating Roles:
The Syntax to create a role is:
CREATE ROLE role_name
[IDENTIFIED BY password];
For example:
--> CREATE ROLE testing [IDENTIFIED BY pwd];
FROMstudent_detailsGROUPBYsubjectHAVINGsubject!='Vancouver'AND
subject!='Toronto';
3)
Sometimesyoumayhavemorethanonesubqueriesinyourmainquery.Tryto
minimizethenumberofsubqueryblockinyourquery.
ForExample:Writethequeryas
SELECTnameFROMemployee
WHERE(salary,age)=(SELECTMAX(salary),MAX(age)
FROMemployee_details)ANDdept='Electronics';
Insteadof:SELECTnameFROMemployee
WHEREsalary=(SELECTMAX(salary)FROMemployee_details)
ANDage=(SELECTMAX(age)FROMemployee_details)
ANDemp_dept='Electronics';
4)
UseoperatorEXISTS,INandtablejoinsappropriatelyinyourquery.
a)UsuallyINhastheslowestperformance.
b)INisefficientwhenmostofthefiltercriteriaisinthesubquery.
c)EXISTSisefficientwhenmostofthefiltercriteriaisinthemainquery.
ForExample:Writethequeryas
Select*fromproductpwhereEXISTS(select*fromorder_itemso
whereo.product_id=p.product_id)
Insteadof:Select*fromproductp
whereproduct_idIN(selectproduct_idfromorder_items)
5)UseEXISTSinsteadofDISTINCTwhenusingjoinswhichinvolvestableshaving
onetomanyrelationship.
ForExample:Writethequeryas
SELECTd.dept_id,d.deptFROMdeptd
WHEREEXISTS(SELECT'X'FROMemployeeeWHEREe.dept=d.dept);
Insteadof: SELECTDISTINCTd.dept_id,d.dept
FROMdeptd,employeeeWHEREe.dept=e.dept;
6)TrytouseUNIONALLinplaceofUNION.
ForExample:Writethequeryas
SELECTid,first_nameFROMstudent_details_class10UNIONALL
SELECTid,first_nameFROMsports_team;
Insteadof:
SELECTid,first_name,subjectFROMstudent_details_class10
UNIONSELECTid,first_nameFROMsports_team;
7)BecarefulwhileusingconditionsinWHEREclause.
ForExample:Writethequeryas
SELECTid,first_name,ageFROMstudent_detailsWHEREage>10;
Insteadof:
SELECTid,first_name,ageFROMstudent_detailsWHEREage!=10;
WritethequeryasSELECTid,first_name,age
FROMstudent_detailsWHEREfirst_nameLIKE'Chan%';
Insteadof:
SELECTid,first_name,ageFROMstudent_details
WHERESUBSTR(first_name,1,3)='Cha';
Writethequeryas
SELECTid,first_name,ageFROMstudent_details
WHEREfirst_nameLIKENVL(:name,'%');
Insteadof:
SELECTid,first_name,ageFROMstudent_details
WHEREfirst_name=NVL(:name,first_name);
Writethequeryas
SELECTproduct_id,product_nameFROMproduct
WHEREunit_priceBETWEENMAX(unit_price)andMIN(unit_price)
Insteadof:
SELECTproduct_id,product_nameFROMproduct
WHEREunit_price>=MAX(unit_price)andunit_price<=MIN(unit_price)
Writethequeryas
SELECTid,name,salaryFROMemployeeWHEREdept='Electronics'ANDlocation
='Bangalore';
Insteadof:
SELECTid,name,salaryFROMemployeeWHEREdept||location=
'ElectronicsBangalore';
Usenoncolumnexpressionononesideofthequerybecauseitwillbeprocessed
earlier.
Writethequeryas
SELECTid,name,salaryFROMemployeeWHEREsalary<25000;
Insteadof:
SELECTid,name,salaryFROMemployeeWHEREsalary+10000<35000;
Writethequeryas
SELECTid,first_name,ageFROMstudent_detailsWHEREage>10;
Insteadof:
SELECTid,first_name,ageFROMstudent_detailsWHEREageNOT=10;
8)UseDECODEtoavoidthescanningofsamerowsorjoiningthesametable
repetitively.DECODEcanalsobemadeusedinplaceofGROUPBYorORDERBY
clause.
ForExample:Writethequeryas
SELECTidFROMemployeeWHEREnameLIKE'Ramesh%'andlocation=
'Bangalore';
Insteadof:
SELECTDECODE(location,'Bangalore',id,NULL)idFROMemployee
WHEREnameLIKE'Ramesh%';
9)Tostorelargebinaryobjects,firstplacetheminthefilesystemandaddthefile
pathinthedatabase.
10)TowritequerieswhichprovideefficientperformancefollowthegeneralSQL
standardrules.
a)UsesinglecaseforallSQLverbs
b)BeginallSQLverbsonanewline
c)Separateallwordswithasinglespace
d)RightorleftaligningverbswithintheinitialSQLverb
Prepared by
Laxminarayana