0% found this document useful (0 votes)
2 views

SQL SYNTAXES

The document provides a comprehensive guide on SQL queries, including commands for creating, inserting, updating, deleting, and altering tables. It also covers various types of joins, set operators, single row functions, and multi-row functions. Each section includes syntax examples for clarity and practical application.

Uploaded by

dpak6661781
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL SYNTAXES

The document provides a comprehensive guide on SQL queries, including commands for creating, inserting, updating, deleting, and altering tables. It also covers various types of joins, set operators, single row functions, and multi-row functions. Each section includes syntax examples for clarity and practical application.

Uploaded by

dpak6661781
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL QUERIES

CREATE TABLE:
CREATE TABLE <TABLE_NAME> (
<COUMN1> DATATYPE,
<COUMN1> DATATYPE
);

INSERT :
INSERT INTO TABLE_NAME VALUES(V1,V2,,,Vn);
INSERT INTO TABLE_NAME VALUES(&COLUMN1,&COLUMN2,,,&COLUMNn);
INSERT INTO TABLE_NAME(COLUMN1,COLUMN2,,,COLUMNn)VALUES(V1,V2,,,,Vn);

RENAME:
RENAME EXISTING_TABLE_NAME TO NEW_TABLE_NAME;

DELETE:
DELETE TABLE <TABLE_NAME>
[WHERE <FILTER_CONDITION>];

ALTER:
TO ADD COLUMN
ALTER TABLE <TABLE_NAME>
ADD COLUMN COLUMN_NAME [CONSTRAITS];

TO DROP A COLUMN:
ALTER TABLE TABLE_NAME
DROP COLUMN COLUMN_NAME;

TO MODIFY THE DATATYPE:


ALTER TABLE TABLE_NAME
MODIFY COLUMN_NAME NEW_DATATYPE;

TO MODIFY NULL/NOT NULL:


ALTER TABLE TABLE_NAME
MODIFY COLUMN_NAME DATATYPE NULL/NOT NULL;

TO ADD A UNIQUE:
ALTER TABLE TABLE_NAME
ADD CONSTRAINTS CONSTRAINT_REF_NAME UNIQUE(COLUMN_NAME);

TO ADD A CHECK:
ALTER TABLE TABLE_NAME
ADD CONSTRAINTS CONSTRAINT_REF_NAME CHECK(CONDITION);
TO ADD A PRIMARY KEY
ALTER TABLE TABLE_NAME
ADD CONSTRAINTS CONSTRAINT_REF_NAME PRIMARY KEY(COLUMN_NAME);

TO ADD A FOREIGN KEY


ALTER TABLE TABLE_NAME
ADD CONSTRAINTS CONSTRAINT_REF_NAME FOREIGN KEY(COLUMN_NAME)
REFERENCES PARENT_TABLE_NAME(COLUMN_NAME);

TO RENAME A COLUMN:
ALTER TABLE TABLE_NAME
RENAME COLUMN EXISTING_COLUMN_NAME TO NEW_COLUMN_NAME;

UPDATE:
UPDATE TABLE_NAME
SET COLUMN_NAME=NEW_VALUE
[WHERE <FILTER_CONDITION>];

UPDATE TABLE_NAME
SET COLUMN_NAME=NEW_VALUE , COLUMN_NAME=NEW_VALUE
[WHERE <FILTER_CONDITION>];

TRUNCATE:
TRUNCATE TABLE <TABLE_NAME.;

DROP TABLE:
DROP TABLE <TABLE_NAME>;

PURGE:
PURGE TABLE <TABLE_NAME>;

FLASHBACK:
FLASHBACK TABLE TABLE_NAME
TO BEFORE DROP;

COMMIT:
COMMIT;

ROLLBACK:
ROLLBACK;

SAVEPOINT:
SAVEPOINT SAVEPOINT_NAME;
ROLLBACK TO SAVEPOINT_NAME;
GRANT:
GRANT SQL_STATEMENT
ON TABLE_NAME
TO USER_NAME;

REVOKE:
REVOKE SQL_STATEMENT
ON TABLE_NAME
FROM USER_NAME;

JOINS
CARTESIAN JOIN/CROSS JOIN
ANSI
SELECT COLUMN-NAME/EXPRSSION
FROM TABLE_NAME1 CROSS JOIN TABLE_NAME2;

ORACLE
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAME1 , TABLE_NAME2;

INNER JOIN/EQUI JOIN


ANSI
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAME1 INNER JOIN TABLE_NAME2
ON <JOIN_CONDITION>;
ORACLE
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAME1 , TABLE_NAME2
WHERE <JOIN_CONDITION>;
NATURAL JOIN
ANSI
SELECT COLUMN-NAME/EXPRESSION
FROM TABLE_NAME1 NATURAL JOIN TABLE_NAME2;

SELF JOIN
ANSI
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAEM T1 JOIN TABLE_NAME T2
ON T1.COLUMN_NAME=T2.COLUMN_NAME;
ORACLE
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAME T1, TABLE_NAME T2
WHERE T1.COLUMN_NAME=T2.COLUMN_NAME;
LEFT OUTER JOIN
ANSI
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAEM1 LEFT [OUTER] JOIN TABLE_NAME2
ON TABLE_NAME1.COLUMN_NAME=TABLE_NAME2.COLUMN_NAME;
ORACLE
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAEM1 , TABLE_NAME2
WHERE TABLE_NAME1.COLUMN_NAME=TABLE_NAME2.COLUMN_NAME(+);

RIGHT OUTER JOIN


ANSI
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAEM1 RIGHT [OUTER] JOIN TABLE_NAME2
ON TABLE_NAME1.COLUMN_NAME=TABLE_NAME2.COLUMN_NAME;
ORACLE
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAEM1 , TABLE_NAME2
WHERE TABLE_NAME1.COLUMN_NAME(+)=TABLE_NAME2.COLUMN_NAME;

FULL OUTER JOIN


ANSI
SELECT COLUMN_NAME/EXPRESSION
FROM TABLE_NAEM1 FULL [OUTER] JOIN TABLE_NAME2
ON TABLE_NAME1.COLUMN_NAME=TABLE_NAME2.COLUMN_NAME;

SET OPERATOR:
UNION:
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE1
UNION
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE2>;

UNION ALL:
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE1
UNION ALL
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE2>;

INTERSECT:
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE1
INTERSECT
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE2>;
MINUS: (EXCEPT)
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE1
MINUS
SELECT <COLUMN1>, <COLUMN2> FROM <TABLE2>;

SINGLE ROW FUNCTIONS:


String Functions
1. LENGTH:
SELECT LENGTH(‘STRING’) FROM <TABLE_NAME>;
2. UPPER:
SELECT UPPER(‘STRING’) FROM <TABLE_NAME>;
3. LOWER:
SELECT LOWER(‘STRING’) FROM <TABLE_NAME>;
4. INITCAP:
SELECT INITCAP(‘STRING’) FROM <TABLE_NAME>;
5. SUBSTR:
SELECT SUBSTR(‘STRING’, START, OCCURANCE) FROM <TABLE_NAME>;
6. REPLACE:
SELECT REPLACE(COLUMN_NAME, ‘OLD_STRING’, ‘NEW_STRING’) FROM <TABLE_NAME>;
7. CONCAT:
SELECT CONCAT(COLUMN1, COLUMN2) FROM <TABLE_NAME>;
OR
SELECT COLUMN1 || COLUMN2 FROM TABLE_NAME;

Number Functions

1. ROUND:
SELECT ROUND(COLUMN_NAME, DECIMAL_PLACES) FROM TABLE_NAME;
2. TRUNC:
SELECT TRUNC(COLUMN_NAME, DECIMAL_PLACES) FROM TABLE_NAME;
3. MOD:
SELECT MOD(COLUMN1, COLUMN2) FROM TABLE_NAME;

Date Functions:
1. SYSDATE:
SELECT SYSDATE FROM DUAL;
2. CURRENT_DATE
SELECT CURRENT_DATE FROM DUAL;
3. ADD_MONTHS
SELECT ADD_MONTHS(COLUMN_NAME, NUMBER_OF_MONTHS) FROM TABLE_NAME;
4. NEXT_DAY:
SELECT NEXT_DAY(COLUMN_NAME, 'DAY_NAME') FROM TABLE_NAME;
5. LAST_DAY:
SELECT LAST_DAY(COLUMN_NAME) FROM TABLE_NAME;

Conversion Functions

1. TO_CHAR:
SELECT TO_CHAR(COLUMN_NAME, 'FORMAT_MODEL') FROM TABLE_NAME;
2. TO_DATE:
SELECT TO_DATE('DATE_STRING', 'FORMAT_MODEL') FROM TABLE_NAME;
3. TO_NUMBER:
SELECT TO_NUMBER(COLUMN_NAME) FROM TABLE_NAME;

General Functions

1. NVL:
SELECT NVL(COLUMN_NAME, 'DEFAULT_VALUE') FROM TABLE_NAME;
2. NVL2:
SELECT NVL2(COLUMN_NAME, 'IF_NOT_NULL', 'IF_NULL') FROM TABLE_NAME;
3. NULLIF:
SELECT NULLIF(COLUMN1, COLUMN2) FROM TABLE_NAME;
4. COALESCE:
SELECT COALESCE(COLUMN1, COLUMN2, COLUMN3, ...) FROM TABLE_NAME;

MUTI ROW FUNCTIONS:

1. COUNT:
SELECT COUNT(COLUMN_NAME) FROM TABLE_NAME;
2. SUM:
SELECT SUM(COLUMN_NAME) FROM TABLE_NAME;
3. AVG:
SELECT AVG(COLUMN_NAME) FROM TABLE_NAME;
4. MIN:
SELECT MIN(COLUMN_NAME) FROM TABLE_NAME;
5. MAX:
SELECT MAX(COLUMN_NAME) FROM TABLE_NAME;

You might also like