sql notes
sql notes
A DBMS requires a query language to enable users to access data. SQL (Structured Query
Language) is the language used by most relational database systems.
DDL (Data Definition Language) – The commands which are used to create and maintain
database are grouped into the class called DDL. Create Table, Drop Table, Alter Table, Grant
privileges etc. comes under DDL.
DML (Data Definition Language) – The commands which are used to manipulate data in one
form or another are grouped into the class called DML. Insert into, Select, Update, etc. comes
under DML.
TCL (Transaction Control Language) – COMMIT, ROLLBACK, SAVEPOINT are the examples
of TCL commands.
Data Type: means the type of value it is going to take. Data types are char(w), integer,
number(w), number(w,p), date.
ALTER TABLE:- is used to change the structure of the table( add new column, change the
data type of column and drop any constraint using the alter command.)
The ADD Clause – Using this, one can add a new column into the table.
ALTER TABLE STUDENT ADD(marks number(5,2));
The INSERT INTO – is used to add a new record to the end of an existing database.
INSERT INTO student VALUES(10, “Gaurav”, “New Friends colony”);
INSERT INTO student(RN, NAME) VALUES(3, “Kavita”);
INSERT INTO command can also be used through parameter substitution. In this, the numeric
column name is used with & (ampersand) sign as it is (eg., &rn) and character column name is
used in single quotation marks with & (eg. ‘&name).
INSERT INTO student VALUES (&rn, ‘&name’,’&address’);
The UPDATE command – to change the value of any row in the table.
UPDATE t_name SET column1=value1 WHERE column=value;
UPDATE student SET marks=90 WHERE name= “Gaurav”;
SELECT * FROM Student; /*it displays all the information with all the fields from
table student.*/
SELECT name, rollno FROM student; /*it displays all the information with the fields
rollno and name from the*/ /*table
student. */
GROUP BY Clause – This groups the rows in the result table by columns that have the same
values, so that each group is reduced to a single row. Each item is separated by a comma.
SELECT COUNT(*), SUM(salary) FROM employee GROUP BY city;
HAVING Clause – it restricts grouped rows that appear in the result table.
SELECT COUNT(*), SUM(salary) FROM employee GROUP BY city HAVING city =
“New Delhi”;
ORDER BY Clause – it determines the order of rows in the result table. Data is placed in the
rows in ascending (ASC, by default) order or descending (DESC) order.
SELECT * FROM employee ORDER BY name;
Putting text in the Query statement – The query output must include some text with it. So to do
so, add text in single quotation mark in the query. For example,
SELECT NAME, ‘Has Scored’, MARKS/5 “PERCENTAGE”, ‘%’ FROM student;
CONDITION BASED ON RANGE - BETWEEN Clause – is used to specify the range of the
column.
SELECT name, city FROM employee WHERE salary BETWEEN 10000 AND 15000;
COLUMN ALIASES : It is used to give a different name to the column in the query output. For
example,
SELECT Rno, Name, Marks/5 “PERCENTAGE” FROM student;