Topic 1: Introduction to SQL
• SQL stands for Structured Query Language.• SQL is a standard computer language for
accessing and manipulating databases• SQL is the set of commands that is recognized
by nearly all RDBMS.
SQL commands can be divided into following categories.
• Data Definition Language (DDL)• Data Manipulation Language (DML) • Transaction Control Language (TCL)
DDL (Data Definition Language) Commands:
• These commands are used for creation; alteration and removal of database objects e.g.: CREATE TABLE, ALTER TABLE, and DROP TABLE.
DML (Data Manipulation Language) Commands:
• These commands are used to add, modify or remove the data contained in the database tables. e.g.: INSERT, DELETE, UPDATE, SELECT etc. are DML commands.
TCL (Transaction Control Language) Commands
• These commands are used to start or end transactions. e.g.: COMMIT, ROLLBACK, ROLL BACK TO etc. are TCL commands.
Topic 2: DDL commands
• Note: All commands given below are based on the table Student:
• Table: Student CREATE is used to create a table. The syntax of this command is:
• create table tablename(column1name datatype [constraint],column2name datatype [constraint], column3name datatype [constraint]);
• Keywords specifying Items that may be created• table • Data Types• char(size) - Fixed length string of characters of the set size. The size
of the string is limited to 255 characters. • date • number(maxsize) - Number with a maximum number of digits
specified by "maxsize". • number(maxdigits,maxright) - A decimal number with a manimum
number of "maxdigits" with "a maximum number of digits to the right of the decimal, "maxright".
• varchar(maxsize) - A character string with variable lingth limited to "maxsize".
• Constraints• Constraints are rules for the column.. Possible values include: • not null - The column values must have a value and cannot be null. • primary key - Each record is uniquely identified by this column. • unique - No two values may be the same in the column
Example
• create table citylist(name varchar(20),state varchar(20),population number(8),zipcode number(5) unique);
STUDENT
Name Roll Class Age Marks
Raghav 101 C12 15 91
Ajay 102 M12 14 92
Gagan 103 C12 15 80
Shika 104 E12 14 85
Delete table:
• Drop table name• Example drop student;
Topic 3:DML command
• INSERT INTO: A row or tuple can be inserted in an existing table with the help of an INSERT command.It is DML command
• Syntax:• Insert into tablename [column list] values
(val1, val2,…)• Note: Values should be inserted in the same
order as table creation.
• Example: Write a command to insert a row in the table student.
• Ans: Insert into student values (‘ahmed’, 105, C12, 16, 98);
• • UPDATE STATEMENT: Update command is
used to make changes to the existing values.
Example 1: Write a command to change the marks from 40 to 86 for
rollno 2
• Ans: Update Student Set marks =86 where rollno = 2;
Example 2: Write a query to change the marks as 95 and grade a A+ for
rollno 31.
• Ans: Update Student set marks=95 and grade=’A+’ where rollno = 31;
Example 3: Write a command to update all the marks by adding 5
marks for all students.
• Ans: Update Student set marks = marks +5;
Example 4: Write a command to change the grade as A for all students
whose marks is greater than 90.• Ans: Update Student set grade=’A’ where
marks > 90;
• Next class
• DELETE COMMAND: This command is used to delete one row or more than one row.
• Example: Write a command to delete the details of students who have scored less than 33 marks.
• Ans: Delete from Student where marks < 33;
• SELECT: The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set).
• Syntax: • SELECT [ALL | DISTINCT] columnname1
[,columnname2]FROM tablename1 [,tablename2][WHERE condition] [ and|or condition...][GROUP BY column-list][HAVING "conditions][ORDER BY "column-list" [ASC | DESC] ]
• Note: SQL statements are not case sensitive. SELECT is the same as select.
• Example 1: Select * from student;• Example 2: Select all from student;• The above two commands display all the
details from the table Student.• • Example 3: Select name, marks from student;• This command will display only the name &
marks columns from the table student.• • Example 4: Select Distinct class from student;
• DISTINCT can be used to select column names and values deleting duplicates.
• Output: • Class• C12• M12• E12
• WHERE clause: The WHERE clause is used to specify a selection criterion.
• To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.
Syntax
SELECT column FROM tableWHERE column operator value
With the WHERE clause, the following operators can be used:
Operator Description= Equal<> Not equal> Greater than< Less than>= Greater than or equal
<= Less than or equalBETWEEN Between an inclusive rangeLIKE Search for a patternIN If you know the exact value you
want to return for at least one of the columns
Example 1: Select name, marks from student where age > 14;
Name Marks
Raghav 90
Gagan 90
Example 2: Write a query to display all details about the students whose age
is 15.• Ans: Select * from Student where age=15;
Example 3: Write a query to display name, roll and class of all students
whose marks is above 90.
• Ans: Select name, roll, class from Student where marks > 90;
Example 4: Write a query to display the name and age of students who
belong to class C12.
• Ans: Select name, age from student where class = ‘C12’;