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

SQL Syntax

The document describes SQL commands for creating tables, adding constraints, modifying tables, performing queries with functions and clauses, updating records, grouping, joining, and aggregating data.

Uploaded by

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

SQL Syntax

The document describes SQL commands for creating tables, adding constraints, modifying tables, performing queries with functions and clauses, updating records, grouping, joining, and aggregating data.

Uploaded by

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

not null

CREATE TABLE Student (


StudentId INTEGER CONSTRAINT Stud_SId_nn NOT NULL,
FName VARCHAR2(10) NOT NULL,
LName VARCHAR2(10));

default
CREATE TABLE Student (
StudentId INTEGER,
FName VARCHAR2(10),
DOJ DATE DEFAULT SYSDATE);

primary key constraint


CREATE TABLE Student (
StudentId INTEGER CONSTRAINT stud_sid_pk PRIMARY KEY,
FName VARCHAR2(10),
ContactNo NUMBER(10));

check constraint
CREATE TABLE Student (
StudentId INTEGER,
FName VARCHAR2(10),
Gender CHAR(1) CONSTRAINT Stud_gender_ck1 CHECK(Gender IN('M', 'F')));

unique constraint
CREATE TABLE Student (
StudentId INTEGER,
FName VARCHAR2(10),
ContactNo NUMBER(10) CONSTRAINT Stud_cno_uk UNIQUE);

foriegn key
CREATE TABLE Marks(
CourseId INTEGER,
StudentId INTEGER CONSTRAINT marks_sid_fk REFERENCES Student(StudentId),
MarksScored DECIMAL(5,2));

alter table
ALTER TABLE Student ADD Address VARCHAR2 (20);

distinct single column


SELECT DISTINCT Dept FROM Employee

where clause
SELECT ID, ENAME FROM Employee WHERE SALARY >= 30000 AND DEPT = 'ETA'

where clause varchar


SELECT Id, EName, Designation FROM Employee WHERE EName = 'James Potter'

like operator
SELECT ID, ENAME FROM Employee WHERE ENAME LIKE '%m%'

update single column


UPDATE Employee SET SALARY = SALARY * 1.10

UPDATE Employee SET SALARY = SALARY * 1.20 WHERE ID = 2

UPDATE Employee SET SALARY = SALARY * 1.3, BONUS = SALARY * 0.30 WHERE ID = 1

numeric function
round
SELECT City, MinTemp, ROUND(MinTemp) as "Round", ROUND(MinTemp,1) as
"RoundTo1Digit" FROM Weather;

ceil,floor,abs
SELECT City, MinTemp, CEIL(MinTemp) AS "Ceiling", FLOOR(MinTemp) AS "Floor",
ABS(MinTemp) as "Absolute" FROM Weather;

character function

concatenating string data


SELECT City, Country, CONCAT(City, Country) "CONCAT", City || Country
"ConcatByOperator", CONCAT(CONCAT(City, ', '), Country) "NestedConcat" FROM
Weather;

length ,upper and lower


SELECT City, LENGTH(City) "LENGTH", LOWER(City) "LOWERCASE", UPPER(City)
"UPPERCASE" FROM Weather;

substr
SELECT City, SUBSTR(City,1,4) FIRST4, SUBSTR(City,2,10) TEN_FROM_2, SUBSTR(City,3)
ALL_FROM_3, SUBSTR(City,7, 2) TWO_FROM_7 FROM Weather;

aggregate function

max,min,sum
SELECT MIN(Salary), MAX(Salary), SUM(Salary) FROM Employee

count with distinct


SELECT COUNT(Dept) Count1, COUNT(DISTINCT Dept) Count2 FROM Employee

count
SELECT COUNT(ID) COUNT_ID, COUNT(*) COUNT_STAR, COUNT(Bonus) COUNT_BONUS FROM
Employee

average
SELECT AVG(Salary) AvgSalary, AVG(Bonus) AvgBonus1, SUM(Bonus) / Count(Bonus)
AvgBonus2 FROM Employee

ascending order
Select ID, ENAME, DOJ, SALARY, DEPT, DESIGNATION FROM Employee ORDER BY DEPT ASC

descending order
Select ID, ENAME, DOJ, SALARY, DEPT, DESIGNATION FROM Employee ORDER BY DOJ DESC

Group by
SELECT Dept, Count(ID) FROM Employee GROUP BY Dept;

Group by multiple column


//in group by it is must to use max or min with digits //
SELECT Dept, Designation, MAX(Salary) FROM Employee GROUP BY Dept, Designation;

inner join
SELECT ID, ENAME, E.COMPID AS E_COMPID, C.COMPID AS C_COMPID, MODEL
FROM Employee E INNER JOIN Computer C ON E.COMPID = C.COMPID

You might also like