SQL and Relational Databases: book back answers

17 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.

← Notes and quiz for SQL and Relational Databases

Choose the best answer (1 Mark)

  1. Which commands provide definitions for creating table structure, deleting relations, and modifying relation schemas.

    • a. DDL
    • b. DML
    • c. DCL
    • d. DQL
    Try it, then show the answer

    a. DDL

    DDL commands define, delete, and modify database structures.

    From the book: “The SQL DDL provides commands for defining relation schemas (structure), deleting relations”

  2. Which command lets to change the structure of the table?

    • a. SELECT
    • b. ORDER BY
    • c. MODIFY
    • d. ALTER
    Try it, then show the answer

    d. ALTER

    ALTER is used to change a table's structure, such as adding or modifying a column.

    From the book: “The ALTER command is used to alter the table structure like adding a column”

  3. The command to delete a table including the structure is

    • a. DROP
    • b. DELETE
    • c. DELETE ALL
    • d. ALTER TABLE
    Try it, then show the answer

    a. DROP

    DROP TABLE deletes the rows and removes the table structure from the database.

    From the book: “If you drop a table, all the rows in the table is deleted and the table structure is removed”

  4. Queries can be generated using

    • a. SELECT
    • b. ORDER BY
    • c. MODIFY
    • d. ALTER
    Try it, then show the answer

    a. SELECT

    The SELECT command is used to query or retrieve data from a database table.

    From the book: “The SELECT command is used to query or retrieve data from a table in the database.”

  5. The clause used to sort data in a database

    • a. SORT BY
    • b. ORDER BY
    • c. GROUP BY
    • d. SELECT
    Try it, then show the answer

    b. ORDER BY

    ORDER BY sorts data in ascending or descending order based on one or more columns.

    From the book: “The ORDER BY clause in SQL is used to sort the data in either ascending or descending”

Part -II — Answer the following questions (2 Marks)

  1. Write a query that selects all students whose age is less than 18 in order wise.

    Try it, then show the answer

    SELECT * FROM Student WHERE Age < 18 ORDER BY Name;

    WHERE selects students younger than 18, and ORDER BY Name displays them in name order. The chapter gives this form of SELECT with WHERE and ORDER BY.

    From the book: “The WHERE clause is used to filter the records. It helps to extract only those records which satisfy a given condition.”

  2. Differentiate Unique and Primary Key constraint.

    Try it, then show the answer

    UNIQUE ensures that no two rows have the same value in the specified columns. A Primary Key uniquely identifies a record and does not allow NULL values; only one field of a table can be set as the primary key.

    Both constraints provide uniqueness. The chapter additionally states that a primary key cannot be NULL and only one field per table can be the primary key.

    From the book: “The primary key does not allow NULL values.”

  3. Write the difference between table constraint and column constraint?

    Try it, then show the answer

    A column constraint applies to an individual field. A table constraint applies to a group of one or more columns and is normally given at the end of the table definition.

    The distinction is whether the constraint applies to one field or to multiple columns together.

    From the book: “Table constraint apply to a group of one or more columns.”

  4. Which component of SQL lets insert values in tables and which lets to create a table?

    Try it, then show the answer

    DML lets users insert values into tables; DDL lets users create tables.

    INSERT is a DML command, while CREATE is a DDL command.

    From the book: “Insert Inserts data into a table”

Part -III — Answer the following questions (3 Marks)

  1. What is a constraint? Write short note on Primary key constraint.

    Try it, then show the answer

    A constraint is a condition applicable on a field or set of fields. A Primary Key constraint declares a field as a primary key, which uniquely identifies a record and does not allow NULL values. Only one field of a table can be set as the primary key.

    The primary key ensures that a record can be uniquely identified and that its key value is not NULL.

    From the book: “Constraint is a condition applicable on a field or set of fields.”

  2. Write a SQL statement to modify the student table structure by adding a new field.

    Try it, then show the answer

    ALTER TABLE Student ADD Address char;

    ALTER TABLE with ADD adds a new column to the existing table. Address is the new field in the chapter's example.

    From the book: “ALTER TABLE Student ADD Address char;”

  3. Write any three DDL commands.

    Try it, then show the answer

    CREATE, ALTER, DROP

    CREATE creates tables, ALTER changes database structure, and DROP deletes tables from the database.

    From the book: “SQL commands which comes under Data Definition Language are: Create To create tables in the database.”

  4. Write the use of Savepoint command with an example.

    Try it, then show the answer

    SAVEPOINT temporarily saves a transaction so that it can be rolled back to that point when required. Example: SAVEPOINT S1; ROLLBACK TO S1;

    A named savepoint marks a transaction state; the ROLLBACK command can return to that state.

    From the book: “The SAVEPOINT command is used to temporarily save a transaction so that you can rollback to the point whenever required.”

  5. Write a SQL statement using DISTINCT keyword.

    Try it, then show the answer

    SELECT DISTINCT Place FROM Student;

    DISTINCT eliminates duplicate rows in the selected results.

    From the book: “The DISTINCT keyword is used along with the SELECT command to eliminate duplicate rows in the table.”

Part -IV — Answer the following questions (5 Marks)

  1. Write the different types of constraints and their functions.

    Try it, then show the answer

    Constraints are conditions applied to a field or a set of fields in a table. They ensure the integrity, accuracy and reliability of the data stored in the database.

    1. Unique constraint: It prevents two rows from having the same value in the specified column. It can be used on a field declared as NOT NULL.
    2. Primary key constraint: It declares a field as the primary key and helps uniquely identify each record. A primary key does not allow NULL values.
    3. Default constraint: It assigns a default value to a field when the user does not provide a value for it.
    4. Check constraint: It sets a limit or condition for the values allowed in a field. Only values satisfying the condition are accepted.
    5. Table constraint: It applies a constraint to a group of fields in a table and is normally given at the end of the table definition.

    Example: In the Student table, Age integer DEFAULT 17 assigns 17 to Age when no value is entered; Age integer CHECK (Age <=19) restricts the age to 19 or less.

    Thus, constraints restrict the data entered into tables and help maintain database integrity.

    From the book: “Constraints ensure database integrity, therefore known as database integrity constraints.”

  2. Construct the following SQL statements in the student table- (i) SELECT statement using GROUP BY clause. (ii) SELECT statement using ORDER BY clause.

    Try it, then show the answer

    The SELECT statement retrieves data from a table. GROUP BY groups rows with identical values, while ORDER BY sorts the retrieved data.

    1. GROUP BY syntax: Use GROUP BY with a SELECT statement to group rows that have identical values in a specified column.
    2. Group students by gender: This statement groups the student records by Gender and returns each gender group.
    3. Count students in each group: Use the aggregate function count(*) with GROUP BY to count students in each gender group.
    4. ORDER BY syntax: Use ORDER BY to sort the selected data by one or more columns. By default, the sort is ascending.
    5. Sort by student name: This statement displays all student records in alphabetical order of their names.
    6. Sort in descending order: Add DESC to sort student names in descending order.

    Example: SELECT Gender, count(*) FROM Student GROUP BY Gender;

    GROUP BY groups student records by a column, and ORDER BY arranges records in the required order.

    From the book: “SELECT Gender FROM Student GROUP BY Gender;”

  3. Write a SQL statement to create a table for employee having any five fields and create a table constraint for the employee table.

    Try it, then show the answer

    A table is a collection of related data entries arranged in rows and columns. The CREATE TABLE command creates a table by specifying its fields, data types and constraints.

    1. Create the employee table: The following SQL statement creates an Employee table with five fields.
    2. Specify field names and data types: The fields are employee number, employee name, designation, date of joining and basic pay. Each field is assigned a suitable data type.
    3. Set a primary key: Employee number is declared as the primary key to uniquely identify each record.
    4. Apply a table constraint: The PRIMARY KEY constraint is written at the end of the table definition and applies to the employee number field.
    5. Enforce non-null values: NOT NULL is applied to the fields so that they must contain values.

    Example: CREATE TABLE Employee (EmpNo integer NOT NULL, EmpName char(20) NOT NULL, Designation char(20) NOT NULL, DateOfJoining char(10) NOT NULL, BasicPay integer NOT NULL, PRIMARY KEY (EmpNo));

    This statement creates the Employee table with five fields and a table-level primary key constraint.

    From the book: “In the above example, the two fields, Firstname and Lastname are defined as Primary key which is a Table constraint.”

Next chapterWorking with CSV Files