You are on page 1of 4

SQL QUERY:

1. INSERT INTO - To add new record


INSERT INTO CHDR (CHDRPFX, CHDRCOY, CHDRNUM, CNTTYPE)
VALUES('CH','1','GA005112','VAT')

2. UPDATE - To update the database


UPDATE CHDR SET CNTTYPE ='CB1', TRANNO= 2 WHERE CHDRNUM = 'GA051123'

3. LIMIT- To Display limited number of records


Select * from CHDR LIMIT 10 - Will display 10 records

4. BETWEEN - For Range


SELECT * FROM CHDR WHERE CHDRNUM BETWEEN 'GA005012' AND 'GA005020'

5. ALIASES - To Display column by other name


Select CHDRNUM AS Contract_Number FROM CHDR

6. SQL INNER JOIN (Simple Join):


SELECT CHDR.CHDRNUM, CHDR.CCDATE, CHDR.PAYRNUM, RISK.RSKTYP FROM CHDR
INNER JOIN RISK ON CHDR.CHDRNUM = RISK.CHDRNO

7. SQL LEFT/LEFT OUTER JOIN:


SELECT CHDR.CHDRNUM, CHDR.CCDATE, CHDR.PAYRNUM, RISK.RSKTYP FROM CHDR
LEFT JOIN RISK ON CHDR.CHDRNUM = RISK.CHDRNO ORDER BY CHDRNUM DESC

8. SQL RIGHT/RIGHT OUTER JOIN:


SELECT CHDR.CHDRNUM, CHDR.CCDATE, CHDR.PAYRNUM, RISK.RSKTYP FROM CHDR
RIGHT JOIN RISK ON CHDR.CHDRNUM = RISK.CHDRNO ORDER BY CHDRNUM DESC
9. FULL OUTER JOIN:
SELECT CHDR.CHDRNUM, CHDR.CCDATE, CHDR.PAYRNUM, RISK.RSKTYP FROM
CHDR FULL JOIN RISK ON CHDR.CHDRNUM = RISK.CHDRNO ORDER BY CHDRNUM DESC
10. UNION: The UNION operator is used to combine the result-set of two or more
SELECT statement, each SELECT statement within the UNION must have the same
number of columns
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2(It
Selects only distinct values)
SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM
table2(It allows duplicate as well)
11. SQL SELECT INTO: This statement copies data from one file and inserts it into a new
file (Without any rec or a Blank file)
SELECT * INTO CHDRBACKUP FROM CHDR
OR
SELECT * INTO CHDRBACKUP FROM CHDR WHERE CNTTYPE = 'VA1'
OR
SELECT CHDR.CHDRNUM, CHDR.CNTTYPE, CHDR.COWNNUM, RISK.RSKTYP INTO CHDRBACKUP
FROM CHDR INNER JOIN RISK ON CHDR.CHDRNUM = RISK.CHDRNO WHERE CHDR.CNTTYPE =
CB1
12.SQL INSERT INTO SELECT: The INSERT INTO SELECT statement copies data from one FILE
and inserts it into an existing FILE (Structure of both the files should be same)
INSERT INTO CHDR1 SELECT CHDRNUM, CNTTYP FROM CHDR WHERE CHDRTYP = SA1
OR
INSERT INTO CHDR1 (CHDRNUM, CNTTYPE) SELECT CHDRNUM, CNTTYP FROM CHDR
WHERE CHDRTYP = SA1

13. SQL CREATE DATABASE: The CREATE DATABASE statement is used to create a
database.
CREATE DATABASE DB_Namr

14. SQL CREATE TABLE: The CREATE TABLE statement is used to create a table in a
database.

CREATE TABLE table_name


(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
)

EX:
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
.
15. SQL Constraints: SQL constraints are used to specify rules for the data in a table

In SQL, we have the following constraints:

NOT NULL - Indicates that a column cannot store NULL value


UNIQUE - Ensures that each row for a column must have a unique value
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a
column (or combination of two or more columns) have a unique identity which helps
to find a particular record in a table more easily and quickly
FOREIGN KEY - Ensure the referential integrity of the data in one table to match
values in another table
CHECK - Ensures that the value in a column meets a specific condition
DEFAULT - Specifies a default value for a column

16. Functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum

Useful Scalar Functions:

UCASE() - Converts a field to upper case


LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed

SELECT COUNT(DISTINCT CHDRNUM) FROM CHDR For Counting Distinct


CHDRNUM

17. Group BY: The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.

SELECT CLAIM, COUNT(CLAIM) FROM CRES WHERE CLAIM IN (SELECT


CLAIM FROM CLAM WHERE ZCCCLM = 'Y') GROUP BY CLAIM
18. SQL HAVING : The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
SELECT CLAIM, COUNT(CLAIM) FROM CRES WHERE CLAIM IN (SELECT
CLAIM FROM CLAM WHERE ZCCCLM = 'Y') GROUP BY CLAIM HAVING
COUNT(CLAIM) > 6

19. SUBSTRING Function: This function is used to extract characters from a text field.
SELECT SUBSTRING(SURNAME,1,4) AS CLIENT FROM CLNT
Or
SELECT SUBSTR(SURNAME,1,4) AS CLIENT FROM CLNT

20. LENGTH Function: This function returns the length of the value in a text field.
SELECT LENGTH(CHDRNUM) FROM CHDR

21. ROUND Function: The ROUND() function is used to round a numeric field to the
number of decimals specified.
SELECT ROUND(TOTPRE,0) AS PREMIUM, CHDRNO, RSKTYP, TRANNO FROM RISK

22. NOW Function: The NOW() function returns the current system date and time.
SELECT NOW() FROM CHDR

SELECT DISTINCT B.ITEMITEM, A.LONGDESC ,SUBSTR(B.GENAREA,1,3) AS


MAJOR_CLASS FROM DESCPF A, ITEMPF B WHERE A.DESCTABL ='T3681' AND
A.DESCPFX = 'IT' AND B.ITEMTABL = A.DESCTABL AND B.ITEMPFX =
A.DESCPFX AND B.ITEMITEM = A.DESCITEM AND SUBSTR(B.ITEMITEM,1,1) =
'V'

You might also like