0% found this document useful (0 votes)
17 views31 pages

DBMS SQL Commands (Autosaved) (Autosaved)

Uploaded by

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

DBMS SQL Commands (Autosaved) (Autosaved)

Uploaded by

Hansika Narang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

DBMS SQL Commands

Dr. Mamta Santosh Nair


DBMS SQL Commands
• Create Table Command
• Insert Command
• Delete Command
• Delete Table Command
• Update Command
• Select Command
Valid Name in SQL (Table names and column names)
Employee id – invalid
Employee_id – valid
Employeeid - valid

Create table student data ---- invalid


create table student_data - valid

; is the terminator – without semi colon , command are not accepted in sql
Create Table command
• This command is used to create e table
• Syntax is –

• CREATE TABLE table_name( column1 datatype, column2 datatype, column3


datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) );
Create table student (Rollno int(3), Name varchar2(30),Marks decimal(2,2) primary
key (Rollno));

• CREATE TABLE is the keyword telling the database system what you want to do. In this case, you
want to create a new table. The unique name or identifier for the table follows the CREATE TABLE
statement.
• Then in brackets comes the list defining each column in the table and what sort of data type it is.
The syntax becomes clearer with the following example.
• Int, Char, Varchar, Varchar2, Varchar3, decimal, Date
Command to create the structure of a table (framework) -Columns

SQL> CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20)
NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );

To see the structure of a table (Describe command


Desc Tablename
Insert Into
• The INSERT INTO statement is used to insert new records in a table. It is possible to write the INSERT
INTO statement in two ways.
• The first way specifies both the column names and the values to be inserted:
• INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
To put Values in few columns
Insert into Customers (id, name, age) values(16,’Ramesh’,20);

• If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table. The INSERT INTO syntax would be as follows:
• INSERT INTO table_name
VALUES (value1, value2, value3, ...);
• Put values for all the columns
Insert into customers values(14, ‘Anil’,20,’Mumbai’,30000.50);
Example
Customer Customer ContactNa Address City PostalCod Country
ID Name me e
89 White Karl 305 - 14th Seattle 98128 USA
Clover Jablonski Ave. S.
Markets Suite 3B
90 Wilman Matti Keskuskat Helsinki 21240 Finland
Kala Karttunen u 45

91 Wolski Zbyszek ul. Walla 01-012 Poland


Filtrowa
68

INSERT INTO Customers2 (CustomerName, ContactName, Address, City,


PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
Create table customers2 (
customer_id int ,
customer_name varchar(20),
Contact_Name varchar2(20),
Address varchar2(30),
City varchar2 (20), postal_code varchar(10), country char(20) primary key (customer_id));
Customer Customer ContactNa Address City PostalCod Country
ID Name me e
89 White Karl 305 - 14th Seattle 98128 USA
Clover Jablonski Ave. S.
Markets Suite 3B
90 Wilman Matti Keskuskat Helsinki 21240 Finland
Kala Karttunen u 45

91 Wolski Zbyszek ul. Walla 01-012 Poland


Filtrowa
68
92 Cardinal Tom B. Skagen 21 Stavanger 4006 Norway
Erichsen
Insert Data Only in Specified Columns

• It is also possible to only insert data in specific columns.


The following SQL statement will insert a new record, but only insert data in the "CustomerName",
"City", and "Country" columns (CustomerID will be updated automatically):

• INSERT INTO Customers (CustomerName, City, Country)


VALUES ('Cardinal', 'Stavanger', 'Norway’);

• To insert data in all the columns

• Insert into customers2 Values (93,


”Anita”, “Rajesh”,’23’,”Delhi”,’100001’,’India’) ;

• Insert into customers2 Values (94,


”Ranjit”, “sharma”,’23,’”Mumbai”,’100001’,”India”) ;
Questions

Write command to put record into customers2 table with following table
customer_id int ,
customer_name varchar(20),
Contact_Name varchar2(20),
Address varchar2(30),
City varchar2 (20), postal_code varchar(10), country char(20) primary key
(customer_id));

Insert following records


Customer_id Customer_Na Contact_Nam Address City Postal_code Country
me e
45 Naina Purohit 35, A block Shimla 100023 India
26 Sharma Raipur 100010 India

67 Rashmi Shukla 49 Mumbai 100001 India


43 Pratik Patil Delhi India

Insert into customers2 values(45, ‘Naina’, ‘Purohit’,’35,A,block’, ’Shimla’ , ‘100023’, ‘India’);


Insert into customers2 (Customer_id, Contact_name, City, Postal_code, Country) values (26,’Sharma’, ‘Raipur’,
‘100010’, ‘India’)
CustomerI CustomerN ContactNa Address City PostalCode Country
D ame me
89 White Karl 305 - 14th Seattle 98128 USA
Clover Jablonski Ave. S.
Markets Suite 3B

90 Wilman Matti Keskuskatu Helsinki 21240 Finland


Kala Karttunen 45

91 Wolski Zbyszek ul. Filtrowa Walla 01-012 Poland


68

92 Cardinal null null Stavanger null Norway

Q. 1To display customer name and city from table


Select Customername, city from customers2 ;
Q. 2To display id and postal code
Select customerid, postalcode from customers2;
Q. 3 Display the name and address of customer sorted on the name
only where customer name is wills
Display the data of person s name naina and city is
Raipur
Select Command (SQL) Structured
Query Command
• The most important and most widely used command.
• It is used for retrieval of records.
• The syntax is
SELECT column list, function(), function(), ...
FROM table
Many clauses can be attached with it to add to functionality

Select * from Tablename; (This displays all the columns all the records from the
table)
• To display all the columns (attributes), display the whole
table
Select * from customers2;
Where Clause
• The SQL WHERE Clause is not a command
• The WHERE clause is used to filter records. To give condition/criteria
• The WHERE clause is used to extract only those records that fulfill a specified condition.
• WHERE Syntax
• SELECT column1, column2, ...
FROM table_name
WHERE condition;
• Example
• SELECT * FROM Customers2
WHERE Country='Mexico’;

• SELECT * FROM Customers2


WHERE CustomerID=1;

• The WHERE clause is not only used in SELECT statement, it is also used in UPDATE, DELETE statement, etc.!
• Select * from customers2 where city=‘New York’ and country = ‘USA’
Perform following questions- table name is customers
• Display the data of person s name naina and city is Raipur

• Display the data of all people with age above 30


Complete Syntax of Select
command
SELECT column list, function(), function(), ...
FROM table1
INNER JOIN table2
...
ON table1.col1 = table2.col2
...
WHERE criteria for row selection

[AND criteria for row selection]


[OR criteria for row selection]

GROUP BY column list


HAVING criteria for function results

ORDER BY column list


Order by clause
• This clause is used for sorting the output
• Syntax
• Select * from <table name> order by <column name>,<column name>
• In ascending order of name
• Select * from customers2 order by customer_name ;
In descending order of name
Select * from customers2 order by customer_name desc;
Group By clause
• It is used for grouping the output.
• Syntax
• Select <function> from <table name> group by <column name>
• Select………group by city; (with group by we can only display function output, and not the columns only group by column can be
displayed) (Sum, Average, max, min, count)
• Q. Display the average fees of each city
• Select city, average(fees) from customers2 group by city order by city;

• City Average(fees)
• Delhi 1234
Mumbai 3547
Chennai 4758
Kolkata 3658
• Q. Display the total marks of every class
select class, sum(marks) from student group by class ;

• Q. Display the class wise fees collection


• Select class , sum(fees) from student group by class order by class;
Functions in select command
• Functions
• Sum
• Count
• Average
• Max
• Min
• Q. To find out no of customers –
• Select count(customerid) from customers2;
• ans-34
Q. To find out sum and average of fees from student file.
Select sum(fees), average(fees) from student ;
ans- 34000, 2890
Select Command (Table Name – Customers2)
1. To display id and country of a person
Ans.
2. To display contact name and city of a person

3. To display id , address of a person whose id is 67


Ans.
4. To display the name and address of a person if he is residing in Mumbai

5. To display the address of Indian people

6. To display the id of a person who are belonging to Bangalore or Chennai


7. To display the contact name of a person for id above 40

8. To display all the details of person whose name is Anil

9. To display details of the people who are from Norway.

10. To display details of people whose id is from 10 to 20


Ans. Select * from customers2 where customer_id>10 and customer_id<20;
Select * from customers2 where customer_id between(10,20);
11. To display city of a person if his name starts with B.
Ans Select city , customername from customers2 where name like ‘B’ ;
Delete Command
• The SQL DELETE Statement
• The DELETE statement is used to delete existing records in a table.
• DELETE Syntax
• DELETE FROM table_name WHERE condition;
• DELETE FROM Customers WHERE CustomerName=‘Wilman Kala’;
13. Delete the record of person whose id is 1002
• Delete from customers where customer_id=1002;
14. Delete the records of people living in Patna
• Delete from customers where city=‘Patna’;
15. Delete the records of British people
Delete from customers2 where country =‘Britain’;
Update Command
• The SQL UPDATE Statement
• The UPDATE statement is used to modify the existing records in a table.
• UPDATE Syntax
• UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
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!
19. Change the city of customer id 1005 to Pune
Update cutomers2 set city =‘Pune’ where id=1005;

20. Change the country and city of person name Mike to Britain and London
Update customers2 set city=‘London’ , country =‘Britain’ where customer_name=‘Mike’;
Example
• UPDATE Customers
SET ContactName = 'Alfred Schmidt',
WHERE CustomerID = 1;
• UPDATE Customers
SET ContactName = 'Alfred Schmidt',
City= 'Frankfurt'
WHERE CustomerID = 1;
• UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
12. Display the list of people sorted alphabetically by their names.
Select * from customers2 order by customer_name;

13. Display the id and name of the people in descending order of id


Select id, customer_name from customers2 order by customer_id desc;

14. Display the total number of customers.


Select count(customer_name) from customers2;

15. Display the total salary of all employees from employee table (emp_id int(2), name char(20), dept, designation, emp_sal, bonus, sales )
Select sum(emp_sal) from employee ;

• 16. Display the average salary of all employees from employee table (emp_id, name, dept, designation, emp_sal, bonus,sales)
• Select average(emp_sal) from employee ;

• 17. Display number of employees in each dept. (dept wise) (every dept)
• Select dept, count(emp_id) from employee group by dept;
• Accounts 5
• Finance 3
• HR 4
Create table student (doj date
Insert into student (‘11/01/2020’
Table to be created

FIRST NAME LAST NAME AGE CITY Region Fees Marks Date of joining Grade
Abhijeet Mishra 37 Delhi East 2300 45 43427 A
Abhijeet Salvi 45 Mumbai West 3000 34 43570 A
Anil Patil 56 Delhi North 3200 46 43575 A
Nagrajan Sheshan 20 Chennai South 3400 42 43225 A
Naina Rastogi 35 Mumbai West 4570 15 44111 B
Priya Agrawal 40 Delhi East 9867 27 43689 B
Priya Mishra 39 Patna West 3456 38 44175 A
Rahul Agrawal 30 Mumbai North 2300 37 43391 A
Rahul Sharma 29 Chennai East 9812 27 43770 B
Renu Dave 55 Chennai East 2367 19 43558 B
Practice Commands. write in the notebook and leave 2 lines blank for the
answers
1. Create the table student
2. Insert 5 records
3. Display the records for the people residing in Mumbai or delhi
Select * from student where city=‘Mumbai’ or City =‘Delhi’;
4. Display the total fees paid in every city
Select city, sum(fees) from student group by city;
5. Display the average age of all people
Select average(age) from student;
6.Display the count of persons in each region in ascending orde of region
Select region,Count (first_name) from student group by region order by region;
7. Change the City to Bangalore for the person whose name is Naina.
Update student set city=‘Bangalore’ where first_name=‘Naina’
8. Change the Region and fees paid to West and 35000 for the person whose name is Anil.
Update student set region=‘West’ ,fees=35000 where first_name=‘Anil’ ;
9. Delete record of Priya Agrawal
Delete from student where first_name=‘Priya’ and Last_name =‘Agrawal’;
10. Delete the records of people from Pune.
Delete from student where city=‘Pune’;
11.Display the average fees paid in every city where average fees is more than 20000
Select city, average (fees) from student group by city having average(fees) > 20000;
12. Display first name and last name of all the students with first name starting with A or last name starting with A.
Select first_name, Last_Name from student where first_name like ‘A’ or Last_Name like ‘A’;
13. Change the marks and city to 45 and Chennai for person with age from 30 to 40
Update student set marks=45, city= ‘Chennai’ where age between 30,40;
• Master file – permanent nature (name, last name, dob, mobile nu,
email)
• Transaction file- volatile data , changes frequently – (attendance,
marks in various tests)
• Having is also another clause
• Which is used for giving condition in group by
• Usually conditions are given using where class
• But in group by u can use having

• Where to use group by clause


• Every
• Each
• City wise, region wise, class wise, dept wise
12. Display the average fees paid in every city where average fees is
more than 20000
13. Display first name and last name of all the students with first name
starting with a or last name starting with
15. Change the marks and city to 45 and Chennai for person with age
from 30 to 40
Questions for practice in Access Set
-1
1. Display the name of students .
Select name from student;
2. Display the rollno and city of students
Select rollno, city from student ;
3. Display the name of students who are from Mumbai
Select name from student where city =‘Mumbai’ ;
4. Display the rollnos of students who have achieved more than 50 in midterm exam in management marks .
Select rollno from student where management_marks >50 and exam =‘Midterm’ ;
Marks1 >50 ; mgmnt_marks
5. Display people who are eligible to vote.
Select * from student where age >=18 ;
6. Display the marks in management and marks in IT if they are first class (above 60%)
Select mgmnt_marks, it_marks from student where mgmnt_marks > 60 and it_marks >60 ;
7. Display the average age in each city for rollno >20 sorted by city list
Select city, avg(age) from student group by city having rollno >20 order by city;
8. Display the number of students in every class output should be sorted on city
Select class, count(rollno) from student group by class order by city ;
9. Change the name of person to Amit where rollno is 8.
Update student set name=‘Amit’ where rollno=8;
10. Delete the records of all the Chennaites.
Delete from student where city=‘Chennai’ ;
11. Delete the records of people who have scored less than 40 marks in midterm exam
Delete from student where marks <40 and exam =‘Midterm’ ;
12. Update the column total by adding management marks and IT marks.
Update student set total=marks_mgmt + marks_it ;

13. Display all the records of students in descending order of their age
Select * from student order by age desc ;
14. Display all the records of students alphabetically arranged first names.
Select * from student order by name ;

15. Display the total of individual student by cacluating total of mngmnt marks and it marks (table structure
student – rollno, name, city, age, mngmnt_marks, it_marks)
Select mngmnt_marks/2 + it_marks/2 as halftotal from student ;
halftotal
23
45
Relationship
• There are 2 types of tables
• Master – contains permanent types of data, (data deosn’t change very
frequently)
• Transaction (contains data which changes or updates frequently)
To establish relationship between 2 tables we need to have one common
column
The common column is primary key in master table and foreign key in
transaction table.
Referential Integrity

• For a primary key in master table there can or can not be matching
record in transaction table
• If a record gets deleted from master file the record should be deleted
from transaction file as well
• If a record gets updated from master file the record should be
updated from transaction file as well
• If a record is added in transaction file it is must that it MUST have a
matching record in master file.

You might also like