1. SQL Queries 1
1. SQL Queries 1
1. JOIN Queries (used to view records from more than one file)
(a). Join or inner join [both are same]
Select * from epf a join cpf b on a.name = b.name
Select * from epf a inner join apf b on a.name = b.name
it will show common records of both file matched on the basis of joined field(name here)
(b). Left Join [it will show all records of first file and only matching records of second file)
Select * from epf a left join cpf b on a.name = b.name
(c). Right Join [it will show all records of second file and only matching records of 1st file)
Select * from epf a right join cpf b on a.name = b.name
(d). Full join (Shows all records of both file in exact matching order)
Select * from epf a full join cpf b on a.name = b.name
(e). Exception join (shows all uncommon records of 1st file only)
Select * from cpf a exception join epf b on a.name = b.name
(f). Select * from cpf a right exception join epf b on a.name = b.name
It will show uncommon records of second file.
Note: - Exception join and left exception join are same, right exception file will show all uncommon records
of second file. In Above examples EPF and CPF are physical files.
2. Substring Query
Select substr(name,1,5) from emp
It will show only initial five characters of name field.
SELECT empname, length(rtrim(EMPNAME)) FROM LCOTTK/TEST1 – length is used to find length of field
4. Concatenated results:
SELECT name, skill1 || ' ' || skill2 || '' || skill3 FROM emp
SELECT name, trim(skill1) || ',' || trim(skill2) || ',' || trim(skill3) FROM emp
8. How to see only 4 or 5 or 6 rows from a file – by using this type of query we can find second or third
highest record similarly second or third or fourth lowest recod
Select * from afl fetch first 4 rows only
SELECT * FROM AFL F1 WHERE RRN(F1) > (SELECT MIN(RRN(F2)) FROM AFL
F2 WHERE F1. NAME = F2.NAME)
DELETE FROM AFL F1 WHERE RRN(F1) > (SELECT MIN(RRN(F2)) FROM AFL
F2 WHERE F1. NAME = F2.NAME)
d. Starting with a
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
g. ending with a
SELECT * FROM Customers WHERE CustomerName LIKE '%a';
18. Restricting any field on first place at the time of records fetching: -
Select name, a.* from cpf a
It will show a specific field name here at first place and then shows all the rest of fields.
19. Alter a table deleting or inserting any field without losing any data
alter table epf add column color char(5) before age
alter table epf drop column color
Note:- we can choose char or binary for data type at the time of field addition
SELECT * FROM EPF WHERE SUBSTR (NAME, LENGTH (TRIM (NAME)), 1) = 'I'
This query will show all the names on file EPF ending with letter ‘I’.