6. SQL Queries 2
6. SQL Queries 2
SQL Queries
1. To fetch all records from file
4. NOT EQUAL TO (Condition) is used to retrieve records which satisfy the not equal condition
5. The BETWEEN operator selects values within a range. The values can be numbers, text, or
dates.
7. The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in a descending order, you can use the DESC keyword.
Select * from lib/file ORDER BY asc/desc
Or
10. The MAX() function returns the largest value of the selected column.
11. The MIN() function returns the smallest value of the selected column.
Note: - Sum and AVG can also be calculated by this function, Sum(age), Avg(age)
12. To find all records of any specific field like all records of min(age), max(age).
13. Select field from file ORDER BY field DESC fetch first 2/3/4 rows only
16. The LIKE predicates searches for strings that have a certain/similar pattern.
18. The RRN function returns the relative record number of a row.
19. CONCATINATION (||) is used to merge or club fields of the record together.
Ex: if there are 3 diff. field like month, date and year then we can club all three fields together
as a single field with help of concatenation.
22. The AVG() function returns the average value of a numeric column.
24. The LENGTH () function returns the length of the value in a text field.
25. To trim all blanks or zeroes from any field we can use trim function.
27. Having condition: It can only be used with group by function : It will give result which
matches - given or required conditions
30. Existing/non existing - Query to find uncommon fields records in both files
Select * from apf where name not in (select name from bpf)
SELECT * FROM APF WHERE SUBSTR (NAME, LENGTH (TRIM (NAME)), 1) = 'H'
result =
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.
The most common type of join is: SQL INNER JOIN (simple join).
An SQL INNER JOIN returns all rows from multiple tables where the join condition is met.
File 1 – APF
File 2- BPF
34. The FULL JOIN keyword is use show all records in matching rows including null.
36. RIGHT JOIN shows all records of rigth table with only matching records of the left table.
37. LEFT EXCEPTION JOIN returns only the rows from the first table that do not have a match
in the second table.
38. RIGTH EXCEPTION JOIN returns only the rows from the second table that do not have a
match in the first table.