0% found this document useful (0 votes)
14 views8 pages

SQL

Uploaded by

rajatkumars965.2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
14 views8 pages

SQL

Uploaded by

rajatkumars965.2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 8

SQL COMMANDS

1. SET UP A DATABASE
mysql> CREATE DATABASE school;
Query OK, 1 row affected (0.03 sec)

2. OPEN DATABASE
mysql> USE school;
Database changed

3. CREATE TABLE Teacher


mysql> CREATE TABLE Teachers (
-> TeacherID INT PRIMARY KEY,
-> Name VARCHAR(50),
-> Subject VARCHAR(30),
-> Salary INT);
Query OK, 0 rows affected (0.04 sec)
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| TeacherID | int(11) | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Subject | varchar(30) | YES | | NULL | |
| Salary | int(11) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+

4. Student
mysql> CREATE TABLE Students (
-> StudentID INT PRIMARY KEY,
-> Name VARCHAR(50),
-> Class INT,
-> Section CHAR(1),
-> Marks INT,
-> TeacherID INT,
-> Email VARCHAR(100),
-> FOREIGN KEY (TeacherID) REFERENCES Teachers(TeacherID));
Query OK, 0 rows affected (0.04 sec)
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| StudentID | int(11) | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Class | int(11) | YES | | NULL | |
| Section | char(1) | YES | | NULL | |
| Marks | int(11) | YES | | NULL | |
| TeacherID | int(11) | YES | MUL | NULL | |
| Email | varchar(100) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+

5. Insert Data into Teachers Table:


mysql> INSERT INTO Teachers (TeacherID, Name, Subject, Salary)
-> VALUES
-> (101, 'Rajesh Iyer', 'Math', 75000),
-> (102, 'Anita Desai', 'English', 70000),
-> (103, 'Manoj Patil', 'Physics', 80000),
-> (104, 'Meera Nair', 'History', 72000);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
6. Insert Data into Students Table:
mysql> INSERT INTO Students (StudentID, Name, Class, Section, Marks, TeacherID,
Email)
-> VALUES
-> (201, 'Aryan Sharma', 10, 'A', 85, 101, 'aryan.sharma@example.com'),
-> (202, 'Riya Mehta', 9, 'B', 78, 102, 'riya.mehta@example.com'),
-> (203, 'Karan Patel', 12, 'A', 92, 103, 'karan.patel@example.com'),
-> (204, 'Megha Gupta', 11, 'C', 88, 104, 'megha.gupta@example.com'),
-> (205, 'Rahul Singh', 10, 'A', 80, 101, 'rahul.singh@example.com');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
7. Display Data

mysql> SELECT * FROM Students;


+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
| StudentID | Name | Class | Section | Marks | TeacherID | Email
|
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
| 201 | Aryan Sharma | 10 | A | 85 | 101 |
aryan.sharma@example.com |
| 202 | Riya Mehta | 9 | B | 78 | 102 |
riya.mehta@example.com |
| 203 | Karan Patel | 12 | A | 92 | 103 |
karan.patel@example.com |
| 204 | Megha Gupta | 11 | C | 88 | 104 |
megha.gupta@example.com |
| 205 | Rahul Singh | 10 | A | 80 | 101 |
rahul.singh@example.com |
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM Teachers;


+-----------+-------------+---------+--------+
| TeacherID | Name | Subject | Salary |
+-----------+-------------+---------+--------+
| 101 | Rajesh Iyer | Math | 75000 |
| 102 | Anita Desai | English | 70000 |
| 103 | Manoj Patil | Physics | 80000 |
| 104 | Meera Nair | History | 72000 |
+-----------+-------------+---------+--------+
4 rows in set (0.00 sec)

8. To find students taught by "Rajesh Iyer" (TeacherID = 101):

mysql> SELECT TeacherID FROM Teachers WHERE Name = 'Rajesh Iyer';


+-----------+
| TeacherID |
+-----------+
| 101 |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM Students WHERE TeacherID = 101;


+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
| StudentID | Name | Class | Section | Marks | TeacherID | Email
|
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
| 201 | Aryan Sharma | 10 | A | 85 | 101 |
aryan.sharma@example.com |
| 205 | Rahul Singh | 10 | A | 80 | 101 |
rahul.singh@example.com |
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
2 rows in set (0.00 sec)

9. Get unique TeacherIDs from Students:


mysql> SELECT DISTINCT TeacherID FROM Students;
+-----------+
| TeacherID |
+-----------+
| 101 |
| 102 |
| 103 |
| 104 |
+-----------+
4 rows in set (0.00 sec)

10. Count students per TeacherID:


mysql> SELECT TeacherID, COUNT(*) AS TotalStudents
-> FROM Students
-> GROUP BY TeacherID;
+-----------+---------------+
| TeacherID | TotalStudents |
+-----------+---------------+
| 101 | 2 |
| 102 | 1 |
| 103 | 1 |
| 104 | 1 |
+-----------+---------------+
4 rows in set (0.00 sec)

11. Calculate average marks for each TeacherID:


mysql> SELECT TeacherID, AVG(Marks) AS AverageMarks
-> FROM Students
-> GROUP BY TeacherID;
+-----------+--------------+
| TeacherID | AverageMarks |
+-----------+--------------+
| 101 | 82.5000 |
| 102 | 78.0000 |
| 103 | 92.0000 |
| 104 | 88.0000 |
+-----------+--------------+
4 rows in set (0.00 sec)

12. Find the Total Salary of All Teachers:


mysql> SELECT SUM(Salary) AS TotalSalary FROM Teachers;
+-------------+
| TotalSalary |
+-------------+
| 297000 |
+-------------+
1 row in set (0.00 sec)
13. Increase the Marks of All Students in Section 'A':
mysql> UPDATE Students
-> SET Marks = Marks + 5
-> WHERE Section = 'A';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0

14. Remove a Student:


mysql> DELETE FROM Students WHERE StudentID = 205;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM Students;


+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
| StudentID | Name | Class | Section | Marks | TeacherID | Email
|
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
| 201 | Aryan Sharma | 10 | A | 90 | 101 |
aryan.sharma@example.com |
| 202 | Riya Mehta | 9 | B | 78 | 102 |
riya.mehta@example.com |
| 203 | Karan Patel | 12 | A | 97 | 103 |
karan.patel@example.com |
| 204 | Megha Gupta | 11 | C | 88 | 104 |
megha.gupta@example.com |
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+
4 rows in set (0.00 sec)

15. Add New Column for Grades:


mysql> ALTER TABLE Students
-> ADD Grades VARCHAR(5);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC Students;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| StudentID | int | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Class | int | YES | | NULL | |
| Section | char(1) | YES | | NULL | |
| Marks | int | YES | | NULL | |
| TeacherID | int | YES | MUL | NULL | |
| Email | varchar(100) | YES | | NULL | |
| Grades | varchar(5) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
8 rows in set (0.03 sec)

16. Rename Email Column:


mysql> ALTER TABLE Students
-> CHANGE COLUMN Email StudentEmail VARCHAR(100);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC Students;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| StudentID | int | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Class | int | YES | | NULL | |
| Section | char(1) | YES | | NULL | |
| Marks | int | YES | | NULL | |
| TeacherID | int | YES | MUL | NULL | |
| StudentEmail | varchar(100) | YES | | NULL | |
| Grades | varchar(5) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

17. Update Teachers' Salary Based on Condition:


mysql> UPDATE Teachers
-> SET Salary = Salary + 5000
-> WHERE TeacherID = 102;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM Teachers;


+-----------+-------------+---------+--------+
| TeacherID | Name | Subject | Salary |
+-----------+-------------+---------+--------+
| 101 | Rajesh Iyer | Math | 75000 |
| 102 | Anita Desai | English | 75000 |
| 103 | Manoj Patil | Physics | 80000 |
| 104 | Meera Nair | History | 72000 |
+-----------+-------------+---------+--------+
4 rows in set (0.00 sec)

18. POWER Function:


mysql> SELECT POW(7, 3) AS Result;
+--------+
| Result |
+--------+
| 343 |
+--------+
1 row in set (0.00 sec)

19. ROUND Function:


mysql> SELECT ROUND(Salary, -3) AS RoundedSalary FROM Teachers;
+---------------+
| RoundedSalary |
+---------------+
| 75000 |
| 75000 |
| 80000 |
| 72000 |
+---------------+
4 rows in set (0.00 sec)

20. Sort Teachers by Name in Ascending Order:


mysql> SELECT * FROM Teachers ORDER BY Name ASC;
+-----------+-------------+---------+--------+
| TeacherID | Name | Subject | Salary |
+-----------+-------------+---------+--------+
| 102 | Anita Desai | English | 75000 |
| 103 | Manoj Patil | Physics | 80000 |
| 104 | Meera Nair | History | 72000 |
| 101 | Rajesh Iyer | Math | 75000 |
+-----------+-------------+---------+--------+
4 rows in set (0.00 sec)

21. Sort Students by Marks in Descending Order:


mysql> SELECT * FROM Students ORDER BY Marks DESC;
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+--------+
| StudentID | Name | Class | Section | Marks | TeacherID | StudentEmail
| Grades |
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+--------+
| 203 | Karan Patel | 12 | A | 97 | 103 |
karan.patel@example.com | NULL |
| 201 | Aryan Sharma | 10 | A | 90 | 101 |
aryan.sharma@example.com | NULL |
| 204 | Megha Gupta | 11 | C | 88 | 104 |
megha.gupta@example.com | NULL |
| 202 | Riya Mehta | 9 | B | 78 | 102 |
riya.mehta@example.com | NULL |
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+--------+
4 rows in set (0.00 sec)

22. Names Starting with 'A':


mysql> SELECT * FROM Students WHERE Name LIKE 'A%';
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+--------+
| StudentID | Name | Class | Section | Marks | TeacherID | StudentEmail
| Grades |
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+--------+
| 201 | Aryan Sharma | 10 | A | 90 | 101 |
aryan.sharma@example.com | NULL |
+-----------+--------------+-------+---------+-------+-----------
+--------------------------+--------+
1 row in set (0.00 sec)

23. Names Starting with 'R' and Ending with 'A':


mysql> SELECT * FROM Students WHERE Name LIKE 'R%A';
+-----------+------------+-------+---------+-------+-----------
+------------------------+--------+
| StudentID | Name | Class | Section | Marks | TeacherID | StudentEmail
| Grades |
+-----------+------------+-------+---------+-------+-----------
+------------------------+--------+
| 202 | Riya Mehta | 9 | B | 78 | 102 |
riya.mehta@example.com | NULL |
+-----------+------------+-------+---------+-------+-----------
+------------------------+--------+
1 row in set (0.00 sec)

24. Get Present Date and Time:


mysql> SELECT SYSDATE() AS CurrentDateTime;
+---------------------+
| CurrentDateTime |
+---------------------+
| 2024-11-27 01:07:18 |
+---------------------+
1 row in set (0.00 sec)

25. Retrieve Data from Linked Tables:


mysql> SELECT Students.Name, Students.Marks, Teachers.Subject
-> FROM Students
-> JOIN Teachers ON Students.TeacherID = Teachers.TeacherID;
+--------------+-------+---------+
| Name | Marks | Subject |
+--------------+-------+---------+
| Aryan Sharma | 90 | Math |
| Riya Mehta | 78 | English |
| Karan Patel | 97 | Physics |
| Megha Gupta | 88 | History |
+--------------+-------+---------+
4 rows in set (0.00 sec)

26. Add a New Column for Phone Numbers:


mysql> ALTER TABLE Students ADD COLUMN PhoneNumber VARCHAR(15);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC Students;


+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| StudentID | int | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Class | int | YES | | NULL | |
| Section | char(1) | YES | | NULL | |
| Marks | int | YES | | NULL | |
| TeacherID | int | YES | MUL | NULL | |
| StudentEmail | varchar(100) | YES | | NULL | |
| Grades | varchar(5) | YES | | NULL | |
| PhoneNumber | varchar(15) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

27. Modify Marks Column Type:


mysql> ALTER TABLE Students MODIFY COLUMN Marks DECIMAL(5, 2);
Query OK, 4 rows affected (0.07 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> DESC Students;


+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| StudentID | int | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Class | int | YES | | NULL | |
| Section | char(1) | YES | | NULL | |
| Marks | decimal(5,2) | YES | | NULL | |
| TeacherID | int | YES | MUL | NULL | |
| StudentEmail | varchar(100) | YES | | NULL | |
| Grades | varchar(5) | YES | | NULL | |
| PhoneNumber | varchar(15) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

28. Add Address Column:


mysql> DESC Students;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| StudentID | int | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Class | int | YES | | NULL | |
| Section | char(1) | YES | | NULL | |
| Marks | decimal(5,2) | YES | | NULL | |
| TeacherID | int | YES | MUL | NULL | |
| StudentEmail | varchar(100) | YES | | NULL | |
| Grades | varchar(5) | YES | | NULL | |
| PhoneNumber | varchar(15) | YES | | NULL | |
| Address | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

29. Rename Marks Column:


mysql> ALTER TABLE Students CHANGE COLUMN Marks FinalMarks DECIMAL(5, 2);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC Students;


+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| StudentID | int | NO | PRI | NULL | |
| Name | varchar(50) | YES | | NULL | |
| Class | int | YES | | NULL | |
| Section | char(1) | YES | | NULL | |
| FinalMarks | decimal(5,2) | YES | | NULL | |
| TeacherID | int | YES | MUL | NULL | |
| StudentEmail | varchar(100) | YES | | NULL | |
| Grades | varchar(5) | YES | | NULL | |
| PhoneNumber | varchar(15) | YES | | NULL | |
| Address | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

30. Select data from two linked tables


mysql> SELECT STUDENTS.FINALMARKS, TEACHERS.SUBJECT FROM STUDENTS,TEACHERS WHERE
STUDENTS.TEACHERID = TEACHERS.TEACHERID;
+------------+---------+
| FINALMARKS | SUBJECT |
+------------+---------+
| 90.00 | Math |
| 78.00 | English |
| 97.00 | Physics |
| 88.00 | History |
+------------+---------+
4 rows in set (0.00 sec)

You might also like