Académique Documents
Professionnel Documents
Culture Documents
2. DESC Command................................................................................................................................1
6. SELECT Command............................................................................................................................7
7. Aggregate Functions.......................................................................................................................16
8. Numeric Functions..........................................................................................................................18
9. String Functions..............................................................................................................................20
1. CREATE TABLE Student(Name An SQL relation is defined using the SELECT * FROM Student;
varchar(20), Age integer(5), Section CREATE TABLE command
varchar(10), Address varchar(30), Marks
float(5));
Output
2. DESC Command
S.NO. Command Explanation Output Statement
Output
3. INSERT INTO Student VALUES ('Tanu', INSERT command is used to load SELECT * FROM Student;
'19', '3CS1', 'Sector - 102', '8.6'); data into relation. The values are
specified in the order in which the
INSERT INTO Student VALUES ('Preeti',
corresponding attributes are listed in
'19', '3CS2', 'Sector - 12', '7.2');
the relation schema.
INSERT INTO Student VALUES ('Raj',
'19', '3CS3', 'Sector - 93A', '8.2');
INSERT INTO Student VALUES
('Ramsagar', '18', '2CS2', 'Sector - 13',
'4.9');
INSERT INTO Student VALUES ('Niyati',
'18', '2CS1', 'Sector - 37', '6.4');
INSERT INTO Student VALUES
Output
4. ALTER TABLE Student ADD EnrlNo ALTER TABLE command is used SELECT * FROM Student;
varchar(10); to add attributes to an existing
relation. All tuples in the relation are
assigned null as the values for the
new attribute
Output
5. ALTER TABLE Student Sets EnrlNo column as primary key SELECT * FROM Student;
ADD PRIMARY KEY (EnrlNo);
Output
Output
7. UPDATE Student SET EnrlNo = '2001' UPDATE command helps to assign SELECT * FROM Student;
WHERE Name = 'Tanu'; values to the attributes or records or
change the same in the predefined
Output
8. UPDATE Student SET EnrlNo = '2007' Will not allow duplicate values in UPDATE Student SET EnrlNo =
WHERE Name = 'Shalini'; primary key EnrlNo '2007' WHERE Name = 'Shalini';
Output
Duplicate entry ‘207’ for key 1
6. SELECT Command
S.NO. Command Explanation Output Statement
9. SELECT Address This statement displays all the rows SELECT Address
under the column Address
FROM Student; FROM Student;
Output
10. SELECT DISTINCT Address This statement displays all the rows SELECT DISTINCT Address
under the column Address and
FROM Student; FROM Student;
removes the duplicate values
Output
11. SELECT ALL Address This statement displays all the rows SELECT ALL Address
under the column Address
FROM Student; FROM Student;
Output
12. SELECT Name, Marks * 10 This statement displays all the rows SELECT Name, Marks * 10
under the column Name and will
FROM Student; FROM Student;
display all the elements of column
Merks by multiplying each element
by 10
Output
13. SELECT Name Displays those names that have SELECT Name
address Sector-93A
FROM Student FROM Student
WHERE Address = 'Sector - 93A'; WHERE Address = 'Sector -
93A';
Output
14. SELECT SUM(Marks / 3), Age HAVING is used in aggregate SELECT SUM(Marks / 3), Age
functions and WHERE is used for
FROM Student FROM Student
general use
GROUP BY Age GROUP BY Age
HAVING SUM(Marks / 3) > '6.0'; HAVING SUM(Marks / 3) >
'6.0';
Output
15. SELECT Name Displays those names that have SELECT Name
address Sector-93A and has EnrlNo
FROM Student FROM Student
2003
WHERE Address = 'Sector - 93A' and WHERE Address = 'Sector -
EnrlNo = '2003'; 93A' and EnrlNo = '2003';
Output
16. SELECT Name, Marks Displays those names and their SELECT Name, Marks
respective marks that have Marks
FROM Student FROM Student
falling between 8.0 and 9.0
WHERE Marks BETWEEN '8.0' AND WHERE Marks BETWEEN '8.0'
'9.0'; AND '9.0';
Output
17. SELECT Name, Marks * 10 AS This statement displays all the rows SELECT Name, Marks * 10 AS
Percentage under the column Name and will Percentage
display all the elements of column
FROM Student; FROM Student;
Marks by multiplying each element
by 10. AS renames the column
Marks * 10 to Percentage.
Output
18. SELECT Name, Marks This statement displays only those SELECT Name, Marks
rows under the column Name and
FROM Student FROM Student
Marks where Name starts with a
WHERE Name LIKE '_h%'; letter, then h and then any number of WHERE Name LIKE '_h%';
letters
Output
19. SELECT Name, Marks This statement displays all the rows SELECT Name, Marks
under the column Name and Marks.
FROM Student FROM Student
The output is printed in descending
ORDER BY Name DESC; order of Name ORDER BY Name DESC;
Output
20. SELECT Name, Address, Marks This statement displays all the rows SELECT Name, Address, Marks
under the column Name, Address
FROM Student FROM Student
and Marks. The output is printed in
ORDER BY Address ASC, Marks DESC; ascending order of Address and ORDER BY Address ASC,
descending order of Marks(if two Marks DESC;
addresses are same then second
condition for arranging Marks in
descending order is followed for
those two addresses)
Output
21. SELECT * INTO StudInfo FROM A new table of name StudInfo is SELECT * INTO StudInfo
Student; created which is an exact replica of FROM Student;
table Student
Output
Output
7. Aggregate Functions
S.NO. Command Explanation Output Statement
23. SELECT SUM(Marks / 8) AS Class_Avg Adds all the elements of Marks and SELECT SUM(Marks / 8) AS
divide the sum by 8. The column Class_Avg
FROM Student;
displayed is named as Class_Avg FROM Student;
Output
24. SELECT MIN(Marks), Age, MAX(Marks) Displays the minimum and SELECT MIN(Marks), Age,
maximum marks obtained, among MAX(Marks)
FROM Student
each age group FROM Student
GROUP BY Age;
GROUP BY Age;
Output
25. SELECT COUNT(*), AVG(Marks), Age Displays the number of rows and the SELECT COUNT(*),
average of marks obtained for each AVG(Marks), Age
FROM Student
age group FROM Student
GROUP BY Age;
COUNT counts all records in a GROUP BY Age;
relation except for the repetition.
COUNT(*) also counts the
exceptions
Output
8. Numeric Functions
S.NO. Command Explanation Output Statement
26. SELECT ABS(-15) FROM Dual; Displays the absolute value of the SELECT ABS(-15) FROM
number inside bracket Dual;
Output
27. SELECT POWER(2,4) FROM Dual; Displays 2 power 4 i.e. 2 * 2 * 2 * 2 SELECT POWER(2,4) FROM
Dual;
Output
28. SELECT ROUND(2.345, 3), ROUND reduces the decimal SELECT ROUND(2.345, 3),
ROUND(2.345, 2), ROUND(2.345, 5) number to the number of places ROUND(2.345, 2),
FROM Dual; specified. Till 4 the number is not ROUND(2.345, 5) FROM Dual;
incremented, after 4 the number is
incremented
Output
29. SELECT SQRT(3), EXP(1), 2 + 10 SQRT – Calculates square root SELECT SQRT(3), EXP(1), 2 +
"SUM" FROM Dual; 10 "SUM" FROM Dual;
EXP – Calculates e power the
number in brackets
SUM – Adds the number shown
Output
30. SELECT GREATEST(2, 5, 3), LEAST(2, GREATEST – Chooses greatest of SELECT GREATEST(2, 5, 3),
5, 3), MOD(24, 2) FROM Dual; the numbers in bracket LEAST(2, 5, 3), MOD(24, 2)
FROM Dual;
LEAST – Chooses least of the
numbers in bracket
MOD – Calculates 24 % 2 and prints
the remainder
Output
9. String Functions
S.NO. Command Explanation Output Statement
Output
32. SELECT SUBSTR('TANU KANVAR', 2, SUBSTR – Extracts the letters that SELECT SUBSTR('TANU
6), ASCII('TAnu'), LENGTH('TAnu') are defined within the bounds KANVAR', 2, 6),
FROM Dual; defined in bracket ASCII('TAnu'),
LENGTH('TAnu') FROM Dual;
ASCII – Calculates the ASCII value
LENGTH – Defines the length of
the word
Output
33. SELECT INITCAP('tAnu') FROM Dual; Converts only the first letter to the SELECT INITCAP('tAnu')
upper case FROM Dual;
Output
Output
34. SELECT * FROM Student For SELECT * the number of SELECT * FROM Student
columns of both the tables should be
UNION UNION
same. Here all the elements of 1st
SELECT * FROM Fees; column are added with the elements SELECT * FROM Fees;
of 1st column of another table in a
new table having attribute name as
that of 1st relation referred
Output
35. SELECT * Joins the two tables where all the SELECT *
elements of left table are included.
FROM student INNER JOIN Fees FROM student INNER JOIN
Only those elements of second table Fees
ON Student.EnrlNo = Fees.EnrlNo; are included. The rest are ignored.
ON Student.EnrlNo =
This statement is equivalent to Fees.EnrlNo;
INTERSECT
Output
36. SELECT * Joins the two tables where all the SELECT *
elements of left table are included.
FROM student LEFT OUTER JOIN Fees FROM student LEFT OUTER
The Values corresponding to those JOIN Fees
ON Student.EnrlNo = Fees.EnrlNo; values if present in second table then
those elements are displayed else ON Student.EnrlNo =
NULL is put Fees.EnrlNo;
Output
37. SELECT * Joins the two tables where all the SELECT *
FROM student RIGHT OUTER JOIN Fees elements of right table are included. FROM student RIGHT OUTER
The Values corresponding to those JOIN Fees
ON Student.EnrlNo = Fees.EnrlNo; values if present in first table then
those elements are displayed else ON Student.EnrlNo =
NULL is put Fees.EnrlNo;
Output
38. SELECT * Joins the two tables where all the SELECT *
elements of both tables are included.
FROM student FULL OUTER JOIN Fees FROM student FULL OUTER
The Values corresponding to those JOIN Fees
ON Student.EnrlNo = Fees.EnrlNo; values if present in both tables then
those elements are displayed else ON Student.EnrlNo =
NULL is put Fees.EnrlNo;
Output
39. DELETE FROM Student Deletes particular rows in a relation SELECT * FROM Student;
WHERE EnrlNo = '2008';
Output
40. DELETE FROM Fees; Removes all the records of the SELECT * FROM Fees;
relation though its attributes still
exist
Deletes the selected/all rows from a
table. Here we can delete set of
records by specifying the condition
in where clause
Output
41. TRUNCATE TABLE Student; Deletes all rows together. This is a SELECT * FROM Student;
faster way to delete the records of a
table than delete command. This
once used cannot be undone
Output
42. DROP TABLE Student; Deletes the whole relation along SELECT * FROM Student;
with its structure.
Output
Table ‘Student’ does not exist