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

It - SQL Queries

Uploaded by

chitra2007smitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

It - SQL Queries

Uploaded by

chitra2007smitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ST.

JOSEPH’S CENTRAL SCHOOL ,


VIJAYANAGAR CLASS X: IT 402 (2022-23)
SQL- is the database language by the use of which we can perform certain operations on the
existing database and also we can use this language to create a database. SQL uses certain
commands like Create, Drop, Insert, etc. to carry out the required tasks.
There are four types of languages:-
1. DDL (Data Definition Language)
2. DML (Data Manipulation Language)
3. DCL(Data Control Language)
4. TCL(Transaction Control Language)

DDL (Data Definition Language)


A data definition language (DDL) is a computer language used to create and modify the
structure of database objects in a database.
Common DDL Statements or commands are:-
1. CREATE: - Used to create database or tables.
2. ALTER: - Used to modify/change structure of table.
3. DROP: - Used to delete database objects.

DML (Data Manipulation Language)


A data manipulation language (DML) is a computer programming language used for adding
(inserting), deleting, and modifying (updating) data in a database. .

Common DML Statements are:-


1. SELECT: - Used to retrieves data from the database.
2. INSERT: - Used to insert new record into the database.
3. DELETE: - Used for deletion of records or rows from the database.
4. UPDATE :- Used for modification of information in the

database Picture for reference


CREATE Statement :

Create statement is used for creating a database or a table in any RDBMS Software.

The general syntax of the create statement is shown below.


CREATE TABLE <TABLENAME> ([column definitions]) [table parameters]);

Example:To create a table, SQL command used is as follows


1) CREATE TABLE DOCTOR(ID INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(50),
DESIGNATION VARCHAR(50),
ADDRESS VARCHAR(120),
PHONE_NUMBER NUMERIC(20),
SALARY NUMERIC(20))

2) CREATE TABLE EMPLOYEE(EMP_ID NUMERIC PRIMARY KEY,


Name varchar(50),
Designation varchar(50),
Dob date,
Login_time time)

[ only for reference


TO create tables in the database by using SQL click on tools SQL type the create
command under COMMAND TO EXECUTE space.
To see the result click on EXECUTE.
ERRORS will be displayed in the STATUS BAR, once the create command is error
free then the table will be created in the database.
You will get a message” Command successfully executed.”]

INSERT statement:

INSERT statement is used to add one or more records to a database.

The general syntax of the insert statement is shown below.


INSERT INTO <table_name><column1, column2, column3...> VALUES <value1,
value2, value3 ...>

Example:
1) insert into
DOCTOR("ID","NAME","DESIGNATION","ADDRESS","PHONE_NUMBER","SALARY")
VALUES('111','MANJUNATH','PEDIATRICIAN','VIJAYANAGAR','12345567','200000')

OR
Insert into DOCTOR
VALUES('111','MANJUNATH','PEDIATRICIAN','VIJAYANAGAR','12345567','200000')

2)INSERT INTO EMPLOYEE1


VALUES('121','RAGHU','TEACHER','1992-04-23','10:30:15')
3)insert into
DOCTOR("ID","NAME","DESIGNATION","ADDRESS","PHONE_NUMBER","SALARY")
VALUES('1431','RAAJU','DENTIST','VIJAYANAGAR,MYSURU,#145','12345567','200000')

4)INSERT INTO EMPLOYEE1 VALUES(‘21’,’HEMAVATHI’,’1222-12-20’,’10:30:2’)

*Note that all the column name should be specified with double inverted quotes(“)
and values with single inverted quotes(‘).

SELECT statement(DML): (Create Query in SQL View )


(click on INSERT -> CHOOSE QUERY (SQL VIEW)-> type the command and press F5)
(Execute one command at a time)
Select statement is used to Retrieve the information from the database.

The general syntax of the select statement is as follows:


SELECT * FROM <table_name> [WHERE] <condition>;

WHERE clause specifies which row to retrieve.


ORDER BY clause specifies an order in which to return the rows

Example:
1) SELECT * FROM DOCTOR;

[the above command will retrieve all the records from the table,* represents ALL]

2) SELECT NAME FROM DOCTOR;


[The above command will display only the details of the column NAME ]

3) SELECT NAME,DESIGNATION FROM DOCTOR;


[The above command will display only NAME AND DESIGNATION details ]

4)select NAME from DOCTOR where DESIGNATION='PEDIATRICIAN'


andADDRESS='VIJAYANAGAR';

OR
select NAME from DOCTOR where DESIGNATION='PEDIATRICIAN' and
ADDRESS='vijayanagar';(this command will not work because of lower case of
'vijayanagar'

5) SELECT ID, SALARY- 500 FROM TEACHER

6)SELECT ID +200 FROM DOCTOR

7) SELECT “ID” * 2 FROM DOCTOR(it works with double quote and also without “ “)

8) SELECT ID,SALARY *2 FROM DOCTOR

9)SELECT SALARY / 2 FROM DOCTOR


(use all mathematical operators)

DISTINCT
The SELECT DISTINCT command returns only distinct (different) values in the result set.

1)SELECT DISTINCT DESIGNATION FROM DOCTOR;

DELETE statement
Delete Statement is used to remove one or more records in a database.

The general syntax of the delete statement is as follows:


DELETE FROM <table_name> [WHERE] <condition>;

EXAMPLES :

1) delete from doctor where ID=121;

[The above command deletes the record or tuple from the table doctor where doctor ID =121]

2) delete from doctor where NAME='PADMA' and DESIGNATION='DENTIST';

[The above command delete the record from doctor table if name is padma and designation of
the doctor is dentist]

3) DELETE FROM DOCTOR;

[In the above command if we omit where clause all the records will be deleted, but
structure remains the same.]
Update statement
Update statement is used for modifying records in a database.

The general syntax of the update statement is as follows:

UPDATE <table_name> SET <column_name> = value [, column_name = value ...]


[WHERE <condition>]

EXAMPLE:
1) update SDetails
set Location = ‘Bhubaneswar’
where Rollno = 14

OR
2) update SDetails set Location = ‘Bhubaneswar’ where Rollno = ‘14’

3) update doctor set address = 'saraswathipuram' where id = 131

4) UPDATE DOCTOR SET REPORTING=10 WHERE ID =131

5) UPDATE DOCTOR SET REPORTING=10.30


6) UPDATE EMPLOYEE1 SET
DOB='2000-03-18' WHERE
EMP_ID= '121'

Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement. The
WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in
the table will be updated!

ALTER statement

The ALTER TABLE command adds, deletes, or modifies columns in a table.

The ALTER TABLE command also adds and deletes various constraints in a table.

Example
ALTER TABLE DOCTOR
ADD REPORTING varchar(225);

ADDING ONE MORE COLUMN TO THE EXISTING TABLE


ALTER TABLE DOCTOR
ADD EMAIL VARCHAR(255);

TO DROP A COLUMN
The following SQL deletes the "Email" column from the "DOCTOR" table:

ALTER TABLE doctor


DROP COLUMN Email, reporting;

ALTER COLUMN command is used to change the data type of a column in a table.

The following SQL COMMAND changes the data type of the column named
"BirthDate" in the"Employees" table to type year:
ALTER TABLE doctor
ALTER COLUMN PHONE_NUMBER VARCHAR(255)

TO change name of a table

ALTER TABLE doctor


rename to emp;
TO ADD CONSTRAINT FOR ALREADY CREATED TABLE

ALTER TABLE DOCTOR

ADD CONSTRAINT PRIMARY KEY(PHONE_NUMBER);

Drop command
The DROP TABLE command deletes a table in the database.

SYNTAX
drop table Tablename

EXAMPLE
drop table doctor;

DROP DATABASE

The DROP DATABASE command is used to delete an existing SQL database.

The following SQL drops a database named "testDB":

Example
DROP DATABASE testDB;
DROP COLUMN

The DROP COLUMN command is used to delete a column in an existing table.

The following SQL deletes the "phonenumber" column from the "doctor" table:

Example
ALTER TABLE doctor
DROP COLUMN phonenumber;

ALTER TABLE TEACHER


DROP PRIMARY KEY;

EXAMPLE 2

ID NAME MARKS ADDRESS AGE


1 RACHANA 85 MYSORE 15
2 SHIRLEY 90 BANGALORE 16
3 AKSHARA 40 BANGALORE 15
4 FATHIMA 60 MYSORE 14
5 RAMYA 70 MYSORE 17
Create the table:
1) CREATE TABLE STUDENT(ID INTEGER NOT NULL PRIMARY KEY,

NAME VARCHAR(50),
MARKS NUMERIC(10),
ADDRESS VARCHAR(10),
AGE NUMERIC(10))
2) SELECT *FROM STUDENT

3) INSERT INTO STUDENT VALUES('001','RACHANA','85','MYSORE','15')


INSERT INTO STUDENT VALUES('002','SHIRLEY','90','BANGALORE','16')
INSERT INTO STUDENT VALUES('003','AKSHARA',’40','BANGALORE','15')
OR
INSERT INTO
STUDENT("ID","NAME","MARKS","ADDRESS","AGE")
VALUES('003','AKSHARA','20','BANGALORE','15')

INSERT INTO
STUDENT("ID","NAME","MARKS","ADDRESS","AGE")
VALUES('004','FATHIMA','60','MYSORE','14')

INSERT INTO
STUDENT("ID","NAME","MARKS","ADDRESS","AGE")
VALUES('005','RAMYA','70','MYSORE','17')

4) SELECT *FROM STUDENT WHERE ADDRESS='MYSORE'

5) SELECT NAME FROM STUDENT

6) SELECT NAME,MARKS FROM STUDENT

7) SELECT NAME FROM STUDENT WHERE ADDRESS='MYSORE' AND AGE='15'

8) SELECT ID, MARKS-5 FROM STUDENT

9) SELECT MARKS +10 FROM STUDENT

10) SELECT MARKS/2 FROM STUDENT

11) SELECT DISTINCT AGE FROM STUDENT

12) SELECT *FROM STUDENT


ORDER BY NAME DESC

13) SELECT *FROM STUDENT


ORDER BY NAME ASC

14) UPDATE STUDENT SET ADDRESS='HUBLI' WHERE ID='1'

15) ALTER TABLE STUDENT


ADD CLASS NUMERIC(10)
NOW SELECT *FROM STUDENT

16) ALTER TABLE STUDENT


DROP COLUMN CLASS
17) ALTER TABLE STUDENT
ALTER AGE VARCHAR(10)
(ABOVE COMMAND IS USED TO CHANGE THE DATA TYPE )

18) ALTER TABLE STUDENT


RENAME TO SCHOOL
(ABOVE COMMAND IS USED TO CHANGE THE TABLE NAME)

19) ALTER TABLE SCHOOL


DROP COLUMN NAME

20) ALTER TABLE SCHOOL DROP PRIMARY KEY

21) DELETE FROM SCHOOL WHERE ID=1

22) DELETE FROM SCHOOL WHERE ADDRESS='BANGALORE' AND AGE='16'

23) DROP TABLE SCHOOL

24) DROP DATABASE 10A

25) Insertquery(SQL View)


(Execute one statement at a time)

You might also like