My SQL Commands
My SQL Commands
Use : The USE Statement is used to select a database and perform SQL operations into that data-
base.
syntax:
use <database name>
show databases: Displays the list of databases on the MySQL server host.
drop database : Drops all tables in the database and deletes the database
syntax:
drop database <database name>
2. Sharmila wants to make the database name ‘COMPANY’ active and display the names of all the
tables in it. Write MYSQL commands for it. [2015 D]
Ans:
create database COMPANY
use COMPANY
show tables
3. Which sql command is used to show the name of currently active database.
Ans : select database()
Create table: The CREATE TABLE statement allows you to create a new table in a database.
syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The following example creates a table called “Student” that contains five columns: Rollno, Name, Age,
Marks, and Gender.
mysql>create table student(rollno int(3),Name char(20),Age int(2),Marks int(3),Gender char(1))
Describe or Desc: DESCRIBE command is used to show the structure of table like column names and
constraints on column name. The DESC is the short form of DESCRIBE command.
The followng example show the structure of above created table ‘student’
mysql> describe student
output
5. Saumya had previously created a table named “Product” in a database using MYSQL.
Later on she forgot the table structure. Suggest to her the suitable MYSQL command through which
she can check the structure of the already created table. [SP 14]
Ans:Desc Product
or
Describe Product
INSERT INTO: The INSERT INTO statement is used to insert new records in a table. We can write
insert into statemet in two ways:
1.When insert data only in specified columns: Specify the column’s name in insert into statement
when you want to insert values in specified columns in table.
Syntax:
insert into <table name>(<columns name>) values(<values>)
for example if a student table having fields(rollno,stuname,age,marks) and the following insert into
statement can be used if you want to insert a record with rollno and age only
insert into student (rollno,age) values(5,18)
NULL value will be stored automatically in rest of the fields i.e. stuname,marks
2. When adding values in all columns: If you want to add values for all the columns of the table then
no need to specify the column names in the SQL query. But make sure the order of the values must be
in the same order as the columns in the table. The syntax would be as follows:
insert into <table name> values(value1,value2,.....)
The following insert into staement adding new record in student table for all fields
insert into student values(6,’aman’,17,89)
6. Rewrite the following SQL statement after correcting error(s). Underline the correction made.
INSERT IN EMP(EMPNO,SALES) VALUE(100,20078.50) [2015 D]
Ans: INSERT INTO EMP (EMPNO,SALES) VALUES(100,20078.50)
Select statement: The SELECT statement is used to select data from one or more tables.
Select * from student;
or
select all from student;
The above statement selects all the fields from the student table. To select specified field(s), give fields
name instead of * or all.
Select name,age from student;
The above staement displays only name and age of all students.
Where clause: The WHERE clause is used to extract only those records that fulfill a specified condi-
tion.
syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The followng example displays the name and age of male students only
Select name,age from student where Gender=’M’:
ORDER BY:
The SQL ORDER BY clause is used to sort the data in ascending or descending order according to one
or more columns.
The following shows the syntax of the ORDER BY clause:
SELECT
select_list
FROM
table_name
ORDER BY
sort_expression [ASC | DESC];
In this syntax:
First, place the ORDER BY clause after the FROM clause.
Second, specify a sort expression(asc or desc) after the ORDER BY clause.ASC option is used to sort
the result set by the sort expression in ascending order and DESC option is used to sort the result set
by the sort expression in the descending order.
Note that the ORDER BY clause uses the ASC option by default.
Delete: The SQL DELETE statement is a used to delete one or more records from a table.
Syntax:
delete from <tablename> [where condition]
where condition is otpional and used to delete specity record(s), If no conditions are provided, all re-
cords in the table will be deleted.
Drop table : The drop table command is used to delete a table (physically) as well as all records
Syntax:
drop table <tablename>
9. Mr. William wants to remove all the rows from Inventory table to release the storage space, but he
does not want to remove the structure of the table. What MySql statement should he use?[2012 D]
Ans: delete from Inventory
10. Ms. Mirana wants to remove the entire content of a table “Backup” alongwith its structure to release
the storage space. What MySql statement should she use? [2012 OD]
Ans : drop table Backup
LIKE : The SQL LIKE clause is used to search for a specified pattern using wildcard operators.The
LIKE condition is used in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.
There are two wildcards used in conjunction with the LIKE operator
Wildcard Explanation
% Allows you to match any string of any length (including zero length)
_ Allows you to match on a single character
These symbols can be used in combination.
Note: If you are pattern matching with char datatypes, remember that chars are padded with spaces
at the end to fill the length of the field. This may give you unexpected results when you use the LIKE
condition to pattern match at the end of a string.
12. The LastName column of a table “Directory” is given below: [2011 SP]
LastName
Batra
Sehgal
Bhatia
Sharma
Mehta
Based on this information, find the output of the following queries:
a) SELECT lastname FROM Directory WHERE lastname like “_a%”;
b)SELECT lastname FROM Directory WHERE lastname not like “%a”;
Ans : (a)
LastName
Batra
(b)
LastName
Sehgal
13. Write SQL query to display employee details from table named ‘Employee’ whose ‘firstname’ ends
with ‘n’ and firstname contains a total of 4 characters (including n). [comptt 17]
Ans :select * from employee where firstname like ‘_ _ _n’
14. Which one of the following SQL queries will display all Employee records containing the word
“Amit”, regardless of case (whether it was stored as AMIT, Amit, or amit etc.)?
(i) SELECT * from Employees WHERE EmpName like UPPER ‘%AMIT%’;
(ii) SELECT * from Employees WHERE EmpName like ‘%AMIT%’ or ‘%Amit%’ or ‘%amit%’;
(iii) SELECT * from Employees WHERE UPPER(EmpName) like ‘%AMIT%’; [D 17]
Ans (iii) SELECT * from Employees WHERE UPPER(EmpName) like ‘%AMIT%’;
NULL : Null or NULL is a special marker used to indicate that a data value does not exist in the da-
tabase. A field with a NULL value is a field with no value. The IS NULL condition is used in SQL to test
for a NULL value.
15. Pooja, a student of class XI, created a table “Book”. Price is a column of this table. To find the details
of books whose prices have not been entered she wrote the following query:
Select * from Book where Price = NULL;
Help Pooja to run the query by removing the errors from the query and rewriting it. [2011 SP]
Ans : Select * from Book where Price is NULL;
DISTINCT: The SQL DISTINCT keyword is used to eliminate all the duplicate records and fetching
only unique records.
BETWEEN : BETWEEN condition is used to retrieve values within a range in a SELECT, INSERT,
UPDATE, or DELETE statement. The values can be numbers, text, or dates. BETWEEN is a shorthand
for >= AND <=. BETWEEN is inclusive, i.e. begin and end values are included.
Syntax:
expression BETWEEN begin value AND end value
For example the following statement displays the records from students table those students have got
marks between 30 and 70(both inclusive).
Select * from student where marks between 30 and 70
or
Select * from student where marks>=30 and marks<=70
OR OPERATOR: The Logical OR operator in MySQL compares two conditions and returns TRUE if
either of the conditions is TRUE and returns FALSE when both are FALSE.
IN OPERATOR : The IN operator allows you to specify multiple values in a WHERE clause.The IN
operator is a shorthand for multiple OR conditions. The “IN” operator is written as the word ‘IN’ followed
by multiple values separated by a comma inside brackets. The “IN” operator evaluates multiple values
on a single data column. If any given value is matched with the column value , it displays that data row.
If none of the values matches, the SQL statement won’t return that data row.
21.Write the following statement using ‘OR’ logical operator : [comptt 17]
SELECT first_name, last_name, subject FROM studentdetails WHERE subject IN (‘Maths’, ‘Sci-
ence’);
Ans: SELECT first_name, last_name, subject FROM studentdetails WHERE subject = ‘Maths’ or sub-
ject= ‘Science’
23. Mrs. Sen entered the following SQL statement to display all Salespersons of the cities “Chennai”
and ‘Mumbai’ from the table ‘Sales’. [D 16]
Table :Sales
Scode Name City
101 Aakriti Mumbai
102 Aman Punjab
103 Banit Delhi
104 Fauzia Mumba
SELECT * FROM Sales WHERE City=’Chennai’ AND City=’Mumbai’;
Rewrite the correct statement if wrong or write “statement is correct”.
Ans : SELECT * FROM Sales WHERE City=’Chennai’ or City=’Mumbai’;
PRACTICAL QUESTIONS
Practice Questions
27 If a database “Employee” exists, which MySql command helps you to start working in that data-
base? [2011 SP]
28. Roli wants to list the name of all the tables in her database name “Gadgets”. Which command(s)
she should use to get the desired result. [SP 14]
29. Write MySql ecommand to delete the database Emp
32. Suggest the suitable command for the following purpose: [SP 18]
i.To display the list of the databases already existing in MySQL.
ii. To use the database named City.
iii.To remove the pre-existing database named Clients.
iv.To remove all the records of the table named “Club” at one go along with its structure permanently
33. Sarthya, a student of class XI, created a table “RESULT”. Grade is one of the column of this table.
To find the details of students whose Grades have not been entered, he wrote the following MySql
query. which did not give the desired result. [D 2011]
SELECT*FROM Result WHERE Grade = “Null”;
Help Sarthya to run the query by removing the errors from the query and write the correct Query.
35. Rewrite the following SQL statement after correcting error(s). Underline the corrections made.
INSERT IN STUDENT(RNO,MARKS) VALUE (5,78.5); [compt 2014]
37. Write SQL statement to delete rows with SCode greater than 701 in Supplier table. [comptt 2020]
38. Charvi wants to delete the records where the “FirstName” is “Rama” in the ‘Emp’ Table. She has
entered the following SQL statement. An error is being displayed. Rewrite the correct statement.
DELETE ‘Rama’ FirstName FROM Emp; [OD 16]
40. Write MySql command will be used to open an already existing database “LIBRARY”. [D 2011]
42. Write MySQl command to display the list of existing databases. [2012 D]
UPDATE(): The UPDATE command in SQL is used to modify or change the existing records in a
table. If you want to update a particular value, then you can use where condition otherwise changes
will take effect in all records.
Syntax:
UPDATE table_name SET col1=val1, col2=val2…[Where condition];
43. In the table “Student”, Priya wanted to increase the Marks (Column Name:Marks) of those
students by 5 who have got Marks below 33. She has entered the following statement: [2018]
SELECT Marks+5 FROM Student WHERE Marks<33;
Identify errors(if any) in the above statement. Rewrite the correct SQL statement.
Ans Error : UPDATE should be used instead of SELECT
Correct SQL statement:
UPDATE Student SET Marks= Marks+5 WHERE Marks<33;
44.Write the UPDATE statement in MySQL to increase commission by 100.00 in the ‘‘Commission’’
column in the ‘Emp’ table. [compt 2014]
Ans: update emp set commission = commission +100
45 Ms. Saloni wants to assign the value 700 as value in Fee Column for all the students in table named
“STUDENT”. She has entered the following SQL statement:
UPDATE STUDENT ASSIGN Fee= 700;
Rewrite the correct statement if there is any error. [comptt 2020]
Ans UPDATE STUDENT SET Fee=700;
46. The ____________command can be used to makes changes in the rows of a table in SQL.
[SP 21]
Ans update
Alter table: The MySQL ALTER TABLE statement is used to add, modify,rename or drop/delete
columns in a table. The MySQL ALTER TABLE statement is also used to rename a table.
Adding new column:We can perform a add column operation using Alter Table command
Syntax :ALTER TABLE table_nameADD column_name column_definition;
For example : Write a command to insert the Game_played column with VARCHAR data type and 30
size into the student table?
Ans : Alter table student add Game_Played varchar(30)
Drop column: SQL has ALTER TABLE DROP COLUMN command for removing columns from an
existing table.
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
For example : The following comand will remove Hobbies column from student table
Alter table student drop Hobbies
48 Structure of table Bank is given below
Table: BANK
FieldName Datatype Size Constraint
Acct_number Integer 4 Primarykey
Name Varchar 3
BirthDate Date
Balance Integer 8 NotNull
Give the answer of following
(a) Write command to add another column Fname of char type with 10 width.
Ans Alter table Bank add Fname char (10)
or
Alter table Bank add column Fname char (10)
49. Sahil created a table in Mysql. Later on he found that there should have been another column in the
table. Which command should he use to add another column to the table? [2011 SP]
Ans : Alter table
50. While creating the table Student last week, Ms. Sharma forgot to include the column Game_Played.
Now write a command to insert the Game_played column with VARCHAR data type and 30 size into
the student table?
51. Write SQL command to remove column named ‘Hobbies’ from a table named ‘student’.
63. Which SQL command is used to add a new attribute in a table? [comptt 2020]
Ans ALTER TABLE
64. Which SQL aggregate function is used to count all records of a table ? [comptt 2020]
Ans COUNT(*)
65. Which clause is used with a SELECT command in SQL to display the records in ascending order of
an attribute? [comptt 2020]
Ans ORDER BY
66. In SQL, name the clause that is used to display the tuples in ascending order of an attribute.
Ans ORDER BY
68. Write any one aggregate function used in SQL. [SP 21]
Ans SUM / AVG / COUNT / MAX / MIN
70. Which key word is used to sort the records of a table in descending order? 1[SP 2019-20]
Ans. DESC
71. Which clause is used to sort the records of a table? 1 [SP 2019-20]
72. Which command is used to modify the records of the table? 1 [SP 2019-20]
73. Which clause is used to remove the duplicating rows of the table? 1 [SP 2019-20]
74. In SQL, write the query to display the list of tables stored in a database. 1[SP 21]
75.Which of the following types of table constraints will prevent the entry of duplicate rows? 1[SP 21]
a) Unique b) Distinct c) Primary Key d) NULL
77. Write the UPDATE command to increase the commission (column name: COMM) by 500 of all the
salesmen who have achieved sales(Column name: SALES) more than 200000. The table’s name is
COMPANY. [2015 OD]
78. In ‘Marks’ column of ‘Student’ table, for Rollnumber 2, the class Teacher entered the marks as 45.
However there was a totaling error and the student has got her marks increased by 5. Write MySQL
command to change the marks in ‘Student’ table. [2015 SP]
79. Consider table ‘Book’. Write SQL statement to change the price for the BOOK titled ‘Informatics
made fun’ to 200? [comptt 16]
Bcode Title Price
101 ABC of Physics 300
102 Informatics made fun 250
103 Computers are simple 375
80. Ms. Shalini has just created a table named “Employee” containing columns Empid, Ename, Depart-
ment, Salary. After creating the table, she realized that she has forgotten to add a primary key column
in the table. Help her in writing SQL command to add a primary key column empid. Also state the im-
portance of Primary key in a table. [SP 18]
82. Rashi wants to add another column ‘Hobbies’ with datatype and size as VARCHAR(50) in the al-
ready existing table ‘Student’. She has written the following statement. However it has errors. Rewrite
the correct statement.
MODIFY TABLE Student Hobbies VARCHAR; [comptt 17]
83.Ariya wants to add another column ‘Gender’ in the already existing table ‘CUSTOMERS’. She has
written the following statement. However , it has errors . Rewrite the correct statement.
MODIFY TABLE CUSTOMERS GENDER char(1); [OD 17] 1
84. Kunal created the following table with the name ‘Friends’: [D 16]
FriendCode Name Hobbies
F101 Bijoy Swimming
F102 Abhinav Reading books
F103 Jyotsna Dancing
Now, Kunal wants to delete the ‘Hobbies’ column. Write the MySQLstatement
93.Which one of the following would arrange the rows in ascending order in SQL ? [comptt 21]
(A) SORT BY (B) ALIGN BY (C) GROUP BY (D) ORDER BY
4 Write MYSQL command through you can check the structure of the already created table “PROD-
UCT”.
(a) Desc Product (b) Select Product (c) Show Product (d) Dispaly Product
5 Write command to display the names of all the tables in current database.
(a) display tables (b) show tables (c) desc tables (d) select tables
6.Write SQL command to remove column named ‘Hobbies’ from a table named ‘student’,
(i) alter table student delete Hobbies (ii) delete hobbies from student
(iii) alter table student drop Hobbies (iv) alter table student drop column Hobbies
Select the correct option
(a) i and ii are correct (b) only i is correct (c) iii and iv are correct (d) i and iii are correct
7 write a command to insert the Game_played column with VARCHAR data type and 30 size into the
student table
(i) Alter table student add Game_Played varchar(30)
(ii) Alter table student add column Game_Played varchar(30)
(iii) Update student add column Game_Played varchar(30)
(iv) Create table student add column Game_Played varchar(30)
Which option is correct
(a) i and ii are correct (b) only i is correct (c) only iii is correct (d) iii and iv are correct
8.Write MySQL command to remove all the rows from Inventory table to release the storage space, but
not to remove the structure of the table.
(a) delete * from Inventory (b)delete from Inventory (c) drop table inventory (d) drop rows inventory
9. Write MySQL statement to remove the entire content of a table “Backup” alongwith its structure to
release the storage space.
(a) delete * from Backup (b)delete from Backup (c) drop table Backup (d) Alter table drop Backup
10. Charvi wants to insert“Sharma” in the “LastName” column of the “Emp” table. Write the SQL state-
ment.
Fields of Emp(Firstname,Lastname)
(a)insert into emp(Lastname) values (‘Sharma’) ; (b)Insert into emp values(‘Sharma’)
(c) update emp set Lastname=’Sharma’ (d) insert into emp(‘Sharma’) values(Lastname)
11. Write SQL statement to change the price for the BOOK titled ‘Informatics’ to 200 in book table?
(a) update book set price=price+200 where title=’Informatics”
(b) insert into book(‘Informatics’) values (200)
(c)update book set price=200 where title=”Informatics”
(d)update book add price=200 where title=”Informatics”
12. Write Mysql command to display all information of students in descending order of name from stud
table
(a)Select name from stud order by desc (b) select name from sud desc order by name
(c) select * from stud order by desc (d) select * from stud order by name desc
13. Write MySQL command to select tuples with some salary from emp table
(a) select * from emp where salary=something (b) select * from emp where salary !=NULL
(c) select * from emp where salary is not NULL (d) select * from emp where salary # NULL
14.Write command to display maximum salary department wise from employee table
employee table(Code, Name, Salary, Deptcode)
(a)select deptcode,Max(salary) from employee group by deptcode
(b) Select deptcode, max(salary) from employee order by deptcode
(c) Select deptcode from employee where salary=max(salary)
(d) Select max(salary) from emplyee deptcodewise
15. Suggest the suitable command to remove the pre-existing database named Clients.
(a) delete database Clients (b) drop Clients (c) drop database Clients (d) Alter table drop Clients
16. Write SQL query to display employee details from table named ‘Employee’ whose ‘firstname’ ends
with ‘n’ and firstname contains a total of 4 characters (including n)
(a)select * from employee where firstname like ‘%n’
(b)select * from employee where len(firstname)=4 and like ‘%n’
(c)select * from employee where right(firstname)=’n’ and len(firstname)=4
(d) select * from employee where firstname like ‘_ _ _n’
17. Write a query to display garment names and codes of those garments that have price in the range
1000.00 and 1500.0 (Both 1000.00 and 1500.00 included) in garment table
(a) select gname,code from garment where 1000 <= price <= 1500
(b) select gname,code from garment where price 1000 to 1500
(c) select gname,code from garment where price between 1000 to 1500
(d) select gname,code from garment where price between 1000 and 1500
18. Write a query to show the information of students those name is either Khan, Yadav or Mishra.
(a)select * from student where name=”Khan”,”Yadav”,”Mishra”
(b)select * from student where name in(“Khan”,”Yadav”,”Mishra”)
(c)select * from student where name=”Khan” or “Yadav” or ‘Mishra”
(d) select * from student where name=”Khan” and “Yadav”and “Mishra”
19. Write mysql query to add another column ‘Hobbies’ with datatype and size as VARCHAR(50) in the
already existing table ‘Student
(i)Alter table student add Hobbies varchar(50)
(ii)modify table Student Hobbies VARCHAR
(iii)alter table student add column Hobbies varchar(50)
(iv)create table student ( hobbies varchar(50))
(a) Only i (b) i and iii both (c) only iii (d) iii and iv both
22. Write a query to count the number of students whose fees is less than 11000
(a) select count(no of students) from student where fees<11000
(b) select count from student where fees<11000
(c)select count(*) from student where fees<11000
(d) select no of student from student where fees<11000
23. .Delete all records those marks are between 30 and 50 from stud table
(a)delete * from stud where marks between 30 and 50
(b)delete all from stud where marks between 30 and 50
(c)delete from stud where marks between 30 and 50
(d)delete * from stud where marks between 30 to 50
24. Write command in MySQL to increase commission by 100.00 in the ‘‘Comm’’ column in the ‘Emp’
table.
(a)alter table emp add comm=comm+100 (b)update table emp set comm=comm+100
(c)update table emp set comm=100 (d)insert into emp(comm) values(100)
25. Write mysql query to list the information whose income is null from family table(Name,age,income)
(a)select information from family where income is null (b)select * from family where income=null
(c)select * from family where income is null (d)select information from family where income
= null
26. Write command to show all information whose name end with “a”(table-student)
(a)select * from student where name end “a” (b)select * from student where name like “a%”
(c)select * from student where name like “%a” (d)select * from student where name like “%a%”
Answers:
1.c 2.d 3.b 4.a 5.b 6.c 7.a 8.b 9.c 10.a 11.c 12.d 13.c 14.a 15.c 16.d 17.d 18.b 19.b 20.c 21.d 22.c 23.c
24.b 25.c 26.c