SQL Programs with Outputs for Class 12
SQL Programs with Outputs for Class 12
To modify data within a specific record in an SQL table, use the UPDATE SQL query. For example, to change the Marks of a student with Roll = 1 to 90, the query would be: UPDATE Students SET Marks = 90 WHERE Roll = 1; This query updates the Marks column for the record with a Roll value of 1. To verify the changes, use a SELECT query such as SELECT * FROM Students WHERE Roll = 1; which will display the updated record, confirming that the Marks have changed to 90 .
The ORDER BY clause in SQL queries is used to sort the result set in either ascending or descending order based on one or more columns. This clause enhances data analysis by organizing output in a meaningful sequence. For instance, the query SELECT * FROM Employee ORDER BY Salary DESC; sorts the employee data in descending order of salary, placing Sneha before Rahul. This ordering helps in quickly identifying the highest or lowest values, improving readability and decision-making .
To display only specific columns, use the SELECT statement specifying the column names. For example, SELECT Name, Salary FROM Employee; retrieves only the Name and Salary columns, omitting others. This is necessary to improve query performance, reduce processing time, and provide users with only relevant information. It is especially useful in large databases where full table retrieval would be inefficient and overwhelming .
Inserting multiple rows in a single SQL query enhances database efficiency by reducing the number of individual database transactions needed. For instance, instead of inserting rows one at a time, INSERT INTO Employee VALUES (101, 'Rahul', 30000), (102, 'Sneha', 35000); performs a batch insertion. This reduces transactional overhead, speeds up the insertion process, and minimizes server load. As a result, data insertion becomes faster and more streamlined, beneficial in environments requiring high-volume inserts .
Specifying column data types when creating SQL tables is critical as it determines the type of data that can be stored, ensuring data accuracy and optimizing storage. For example, using INT for numeric values and VARCHAR for strings ensures that the database manages operations such as searching and indexing efficiently. Poor choices can lead to data truncation, inefficient queries, and increased storage, impacting overall database performance and data integrity .
Ensure only certain entries are selected by using the WHERE clause in an SQL query. For instance, to select employees with a salary greater than 32000, the query would be: SELECT * FROM Employee WHERE Salary > 32000; This retrieves only the rows where the condition is met, as seen with Sneha, who has a salary of 35000. This filtering enhances data retrieval efficiency by returning only relevant data, thereby reducing data processing overhead .
Insertion operations using the INSERT INTO SQL statement add new rows to database tables, enhancing the table's data volume and diversity. For instance, INSERT INTO Employee VALUES (101, 'Rahul', 30000), (102, 'Sneha', 35000); adds these new records to the Employee table. Verify the operation with a SELECT query like SELECT * FROM Employee; which should show the newly inserted rows confirming successful data addition .
Updating SQL database records carries risks such as unintentional data loss or corruption, particularly if conditions in the WHERE clause are incorrect, potentially affecting multiple rows. These risks can be mitigated by thorough testing in a development environment, using transaction management to rollback unwanted changes, and consistently backing up data. Implementing measures such as logging changes and using precise WHERE conditions can also prevent unintended updates .
Deleting data from a table with an SQL DELETE query permanently removes the specified records from the database. For example, DELETE FROM Students WHERE Roll = 1; removes the student record with Roll number 1. This action reduces data storage usage but demands caution as the data cannot be recovered easily unless backups are available. To verify deletion, a SELECT query such as SELECT * FROM Students WHERE Roll = 1; can confirm the absence of the record, indicating successful deletion .
Create a new SQL table using the CREATE TABLE statement. Consider column names, data types, and constraints that define the structure and integrity of the table. For example, CREATE TABLE Students (Roll INT, Name VARCHAR(20), Marks INT); creates a table with columns for Roll, Name, and Marks, specifying their data types as INT for numbers and VARCHAR for strings. It is important to choose appropriate data types and constraints to ensure data accuracy and efficiency .