EX. NO.
21:
STRUCTURED QUERY LANGUAGE
1. Create a student table with student id, name and marks as attributes where the student
id is the primary key.
Query:
CREATE DATABASE STUDENT1;
USE STUDENT1;
CREATE TABLE STUD(
STUID INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
MARKS DECIMAL(5,2),
CLASS VARCHAR(5) DEFAULT ‘XII’
);
Output:
2. Write SQL query to insert the details of new students in the above table.
Query:
INSERT INTO stud VALUES(101,'RAMYASREE',98.2);
INSERT INTO stud VALUES(102,'PAVITHRA',83);
INSERT INTO stud VALUES(103,'KEERTHANA',76);
INSERT INTO stud VALUES(104,'THRISHA',90);
INSERT INTO stud VALUES(105,'PIYAS',93);
INSERT INTO stud VALUES(106,'RAGHU',96.7);
OUTPUT:
3. Write SQL query to delete the details of a student in the above table.
Query:
DELETE FROM stud WHERE stuid = 102;
Output:
4. Write SQL query using select command to get the details of the students with marks
more than 80.
QUERY:
SELECT * FROM stud WHERE marks > 80;
OUTPUT:
5. Write SQL query to find the min, max, sum and average of the marks in a student marks
table.
QUERY:
SELECT MAX(marks), MIN(marks), SUM(marks), AVG(marks) FROM stud;
OUTPUT:
6. Write SQL query to order the(student ID, marks) table in descending order of the
marks.
QUERY:
SELECT * FROM stud ORDER BY marks DESC;
OUTPUT:
7. Write SQL query to display
i) the marks without decimal places
ii) display the reminder after dividing the marks by 3 and
iii) display the square of marks.
QUERY:
SELECT ROUND(marks,0), MOD(marks,3), POW(marks,2) FROM stud;
OUTPUT:
8. Write SQL query to display
i. names into capital letters
ii. small letters
iii. display first 3 letters of a name
iv.display last 3 letters of name
v. display the position the letter A in name.
QUERY:
SELECT UCASE(name), LCASE(name), LEFT(name, 3) , RIGHT(name, 3), INSTR(name,'a')
FROM stud;
OUTPUT:
9. Write a SQL query to remove extra spaces
i. from left,
ii. from right
iii. and both sides from the text –
“ Informatics Practices Class XII “
Query:
SELECT LTRIM(" Informatics Practices Class XII ") "left spaces",
RTRIM(" Informatics Practices Class XII ") "right spaces",
TRIM(" Informatics Practices Class XII ");
Output:
10. Write a SQL query to display today’s date in “DATE/MONTH/YEAR” format.
Query:
SELECT CONCAT(DATE(NOW()),CONCAT("/",CONCAT(MONTH(NOW()), CONCAT("/",
YEAR(NOW())))));
Output:
11. Write a SQL query to display day name, month name, day, day of month, day of year
for today’s date.
QUERY:
SELECT DAYNAME(NOW()), MONTHNAME(NOW()), DAY(NOW()),
DAYNAME(NOW()), DAYOFMONTH(NOW()), DAYOFYEAR(NOW());
OUTPUT:
12. Write a SQL query to display the student id, class, name of all the students, whose
name starts with ‘AN’ and names that end with ‘I' .
QUERY:
SELECT STUID,CLASS,NAME FROM STUD WHERE NAME LIKE ‘S%’;
SELECT STUID,CLASS,NAME FROM STUD WHERE NAME LIKE ‘%SH';
OUTPUT:
13. Create a new table (customer id, customer name, country) and find the total number of
customers from each country in the table (customer id, customer name and country) using
group by.
QUERY:
CREATE TABLE customer(
custid INT,
cname CHAR(30),
country CHAR(20)
);
INSERT INTO customer VALUES(301,'SRAVAN','NETHERLANDS');
INSERT INTO customer VALUES(302,'BALAJI','SCOTLAND');
INSERT INTO customer VALUES(303,'HEMACHANDAR','NETHERLANDS');
INSERT INTO customer VALUES(304,'KARTHIKEYAN','SCOTLAND');
INSERT INTO customer VALUES(305,'MYTHILI','ALASKA');
INSERT INTO customer VALUES(306,'PUGITHA','CANADA');
INSERT INTO customer VALUES(307,'NITHYASREE','ALASKA');
SELECT * FROM customer;
SELECT country,count(*) "TOTAL CUSTOMER" FROM customer GROUP BY country;
14. Write a SQL query to
i. select the distinct customer from countries
ii. display the count of distinct customers
iii. display the customer by their countries in ascending order.
QUERY:
SELECT DISTINCT country FROM customer;
SELECT COUNT(DISTINCT country) FROM customer;
SELECT * FROM customer ORDER BY country ASC;
OUTPUT:
15. Write a SQL query to display the details of the customer from the same countries.
QUERY:
SELECT * FROM customer GROUP BY country HAVING COUNT(country)=2;
OUTPUT:
Result:
The SQL query is executed successfully and the output is shown