sql code
sql code
DDL Commands – These commands are used for changing the structure of the
database or table.
Examples are : CREATE, ALTER TABLE, DROP
DML Commands – These commands are used to change the data in the table. It
consists of the query and update commands.
Examples are : SELECT, UPDATE, DELETE, INSERT INTO
Create a table-
Create table Stud ( Sno int, Names varchar(25), Age int , Class int);
Remove a table-
Drop table Stud;
Note-
• Character, date and time should be enclosed in quotes.
• Numeric values should not be enclosed in quotes.
Retrieve records from the table and to display it-
SELECT * FROM Student ;
Select names, age from student;
Here * Represents ALL
Arithmetic Operators
• Arithmetic operators are + - * /
• It is used to perform mathematical calculations.
• Select 7 + 76;
• SELECT Age+5 FROM Stud;
WHERE clause
• It is used to filter records.
• It extracts only those records that fulfill a certain condition.
• Select * from stud where Class>10;
• Select * from stud where Names=’Ajay’;
UPDATE STATEMENT
• It is used to modify the existing data in the table.
• UPDATE stud SET Class = 12 WHERE names = “Paul”;
DELETE STATEMENT
• Used to DELETE one or more rows from a table.
• It removes the entire row.
• Delete from stud where Sno=6;
• To remove all the rows of the table use,
DELETE from stud;
SORTING THE RESULTS- ORDER BY
• ORDER BY is used to display the results of the select statement in ascending or
descending values.
• Select * from stud order by age;
• To display in descending order:
Select * from stud order by age desc;