0% found this document useful (0 votes)
257 views4 pages

SQL Queries for Class 12 CBSE Computer Science

The document provides a comprehensive set of SQL queries and table structures based on the Class 12 CBSE Computer Science syllabus, covering table creation, data manipulation, and data retrieval. It includes examples of various SQL operations such as INSERT, SELECT, UPDATE, DELETE, and JOIN queries. Additionally, it demonstrates the use of aggregate functions, conditions, sorting, and grouping in SQL.

Uploaded by

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

SQL Queries for Class 12 CBSE Computer Science

The document provides a comprehensive set of SQL queries and table structures based on the Class 12 CBSE Computer Science syllabus, covering table creation, data manipulation, and data retrieval. It includes examples of various SQL operations such as INSERT, SELECT, UPDATE, DELETE, and JOIN queries. Additionally, it demonstrates the use of aggregate functions, conditions, sorting, and grouping in SQL.

Uploaded by

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

Here’s a complete set of SQL queries with table structure based on the Class 12 CBSE

Computer Science syllabus. This includes:

 Table creation
 Data manipulation (INSERT, UPDATE, DELETE)
 Data retrieval (SELECT)
 Functions, conditions, sorting, grouping, etc.

✅ Sample Table: Student


CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(30),
Class INT,
Marks INT,
City VARCHAR(30)
);

🔹 Insert Records
INSERT INTO Student VALUES (101, 'Ravi', 12, 87, 'Delhi');
INSERT INTO Student VALUES (102, 'Anu', 11, 92, 'Mumbai');
INSERT INTO Student VALUES (103, 'Kiran', 12, 74, 'Kolkata');
INSERT INTO Student VALUES (104, 'Meena', 11, 65, 'Chennai');
INSERT INTO Student VALUES (105, 'Raj', 12, 56, 'Delhi');

🔍 Basic SELECT Queries


-- 1. Display all records
SELECT * FROM Student;

-- 2. Display only names and marks


SELECT Name, Marks FROM Student;

-- 3. Display students from Delhi


SELECT * FROM Student WHERE City = 'Delhi';

-- 4. List students who scored above 70 marks


SELECT * FROM Student WHERE Marks > 70;

-- 5. Students of Class 12
SELECT Name FROM Student WHERE Class = 12;

⚙️Using Conditions
-- 6. Students with marks between 60 and 90
SELECT * FROM Student WHERE Marks BETWEEN 60 AND 90;

-- 7. Students whose name starts with 'R'


SELECT * FROM Student WHERE Name LIKE 'R%';
-- 8. Students not from 'Mumbai'
SELECT * FROM Student WHERE City <> 'Mumbai';

-- 9. Class is 11 or 12
SELECT * FROM Student WHERE Class IN (11, 12);

🔢 Aggregate Functions
-- 10. Count of total students
SELECT COUNT(*) FROM Student;

-- 11. Maximum marks


SELECT MAX(Marks) FROM Student;

-- 12. Average marks


SELECT AVG(Marks) FROM Student;

-- 13. Total marks of Class 12 students


SELECT SUM(Marks) FROM Student WHERE Class = 12;

🔁 GROUP BY and HAVING


-- 14. Count of students in each city
SELECT City, COUNT(*) FROM Student GROUP BY City;

-- 15. Average marks by class


SELECT Class, AVG(Marks) FROM Student GROUP BY Class;

-- 16. Cities with more than 1 student


SELECT City, COUNT(*) FROM Student GROUP BY City HAVING COUNT(*) > 1;

🔃 ORDER BY
-- 17. Display students sorted by name
SELECT * FROM Student ORDER BY Name;

-- 18. Students sorted by marks (high to low)


SELECT * FROM Student ORDER BY Marks DESC;

✏️UPDATE, DELETE Queries


-- 19. Increase marks by 5 for all students
UPDATE Student SET Marks = Marks + 5;

-- 20. Change city of student 'Meena' to 'Hyderabad'


UPDATE Student SET City = 'Hyderabad' WHERE Name = 'Meena';

-- 21. Delete student with marks less than 60


DELETE FROM Student WHERE Marks < 60;
🧱 ALTER Table
-- 22. Add phone number column
ALTER TABLE Student ADD Phone VARCHAR(15);

-- 23. Drop column phone


ALTER TABLE Student DROP COLUMN Phone;

❌ Drop Table
-- 24. Delete the entire table
DROP TABLE Student;

✅ Tables for JOIN Queries


🔹 Table 1: Student
CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(30),
Class INT,
City VARCHAR(30)
);

🔹 Table 2: Marks
CREATE TABLE Marks (
RollNo INT,
Subject VARCHAR(20),
Marks INT,
FOREIGN KEY (RollNo) REFERENCES Student(RollNo)
);

📝 Sample Data
Insert into Student
INSERT INTO Student VALUES (101, 'Ravi', 12, 'Delhi');
INSERT INTO Student VALUES (102, 'Anu', 11, 'Mumbai');
INSERT INTO Student VALUES (103, 'Kiran', 12, 'Kolkata');
INSERT INTO Student VALUES (104, 'Meena', 11, 'Chennai');

Insert into Marks


INSERT INTO Marks VALUES (101, 'Maths', 87);
INSERT INTO Marks VALUES (101, 'English', 75);
INSERT INTO Marks VALUES (102, 'Maths', 92);
INSERT INTO Marks VALUES (103, 'English', 68);
INSERT INTO Marks VALUES (104, 'Maths', 80);
🔹 4. JOIN with Condition
SELECT [Link], [Link], [Link]
FROM Student
INNER JOIN Marks ON [Link] = [Link]
WHERE [Link] > 80;

🔹 5. JOIN with ORDER BY


SELECT [Link], [Link], [Link]
FROM Student
INNER JOIN Marks ON [Link] = [Link]
ORDER BY [Link] DESC;

✅ Explanation: Displays all joined data sorted by marks in descending order.

Common questions

Powered by AI

Constraints such as primary keys and foreign keys are crucial for maintaining data integrity in SQL databases. A primary key, like `RollNo` in both the `Student` and `Marks` tables , uniquely identifies each record, preventing duplicate entries and ensuring each student or record is distinct. The foreign key in the `Marks` table references the `Student` table, ensuring that all mark entries correspond to an existing student. This linkage prevents orphan records and maintains relational accuracy across tables.

Using the `HAVING` clause is beneficial when filtering aggregated data, as it operates on grouped results after aggregate computations, unlike `WHERE` which filters rows before grouping. For example, identifying cities with more than one student requires using `HAVING` because it evaluates `COUNT(*)` of groups: `SELECT City, COUNT(*) FROM Student GROUP BY City HAVING COUNT(*) > 1` . This facilitates data analysis on summary statistics rather than individual records, which `WHERE` cannot directly achieve.

Dynamically altering the `Student` table by adding or dropping columns can have multiple effects, including modifying the schema's structure to accommodate new data types or eliminating unnecessary ones. Adding a `Phone` column `ALTER TABLE Student ADD Phone VARCHAR(15);` allows storing additional contact information, enhancing data completeness. Conversely, removing a column can simplify the dataset but may lead to data loss if not backed up or referenced elsewhere, potentially affecting queries or applications depending on that column.

Advanced SQL sorting techniques enhance data presentation by organizing and prioritizing information in a meaningful order. Techniques like using `ORDER BY Marks DESC` or `ORDER BY Name` allow for viewing top-scoring students or listing them alphabetically . Combining these with conditions or multiple columns, such as sorting primarily by class and secondarily by marks, provides structured views useful in academic reports, facilitating easier data interpretation and trend spotting.

Deleting records using conditions, such as `DELETE FROM Student WHERE Marks < 60` , removes specific datasets based on defined criteria, aiding in maintaining relevant and clean data. This process is significant for data accuracy, freeing up storage, and eliminating outliers or obsolete records, which can otherwise skew analyses. However, it requires careful consideration of cascading effects on relational integrity, particularly if removed data are referenced elsewhere or necessary for historical records.

SQL JOIN operations can merge student personal data with academic performance by matching rows from the `Student` and `Marks` tables using common keys. For example, an INNER JOIN can be used to include only records with matching RollNo in both tables: `SELECT Student.Name, Marks.Subject, Marks.Marks FROM Student INNER JOIN Marks ON Student.RollNo = Marks.RollNo` . This displays each student's name alongside their subject results, applicable for students enrolled in subjects listed in the Marks table.

Applying a global update of increasing all students' marks by a fixed value, such as `UPDATE Student SET Marks = Marks + 5` , would uniformly shift the distribution of marks. It would inflate all individual scores equally, slightly increasing the average without affecting the relative ranking among students since their positional differences remain the same. However, it might impact thresholds used in conditional queries or grouping, influencing any established criteria based on marks, such as passing grades or eligibility quantiles.

To analyze the distribution and performance of students by their marks and city, aggregate functions, and grouping can be applied. Firstly, to count the number of students in each city, you can use `SELECT City, COUNT(*) FROM Student GROUP BY City` . For performance analysis, `SELECT Class, AVG(Marks) FROM Student GROUP BY Class` provides average marks by class . To identify cities with more than one student, you can run `SELECT City, COUNT(*) FROM Student GROUP BY City HAVING COUNT(*) > 1` . This approach helps in understanding the representation of students across different demographics.

Grouping and conditional expressions enhance data organization by facilitating analytical comparisons and detailed summaries. With `GROUP BY`, like in `SELECT Class, AVG(Marks) FROM Student GROUP BY Class` , data is aggregated for comparative insights among groups. Conditional expressions, such as using `WHERE` clauses for specific mark ranges or cities, refine these groups further to include relevant subcategories or produce conditionally tailored summaries, enabling targeted academic performance analyses.

SQL subqueries improve the evaluation of student data by allowing more complex queries that derive new data or selection criteria based on existing tables. For instance, a subquery can find students scoring above a calculated average, enhancing derived insights. This layered querying method, for example, helps in statistics like identifying students whose marks are above average obtained from a subquery `SELECT AVG(Marks) FROM Student`, which can then be compared with `Marks` for more informative reports.

You might also like