Introduction to Chapter Structured Query 8 Language (SQL)
Introduction to Chapter Structured Query 8 Language (SQL) — Study Notes
NCERT-aligned · 8 notes · 3 shown free
8.1 INTRODUCTION
Explanation8.1 INTRODUCTION
In the previous chapter, we learned about the Relational Database Management System (RDBMS) and its purpose. RDBMS is a software system that allows us to create and manage databases consisting of relations (tables) and to link one or more relations for efficient querying, storage, retrieval, and manipulation of data. There are many RDBMS software available such as MySQL, Microsoft SQL Server, PostgreSQL, Oracle, etc. Each of these supports the creation and management of relational databases. In this chapter, we focus on learning how to create a database, populate it with data, and query it using MySQL, a popular open-source RDBMS software. We will learn the Structured Query Language (SQL), which is the standard language used to interact with relational databases. The chapter covers the creation of a database named StudentAttendance, which was identified in the previous chapter, and demonstrates how to create tables, insert data, manipulate data, and retrieve data using SQL commands. This chapter lays the foundation for understanding database management through SQL, enabling efficient data handling in relational databases.
- RDBMS allows creation and management of relational databases.
- Popular RDBMS include MySQL, Oracle, SQL Server, PostgreSQL.
- SQL is the standard language to interact with relational databases.
- This chapter focuses on MySQL and SQL commands.
- We will create, populate, and query the StudentAttendance database.
- Understanding SQL is essential for managing relational databases.
- 📌 Relational Database Management System (RDBMS): Software for managing relational databases.
- 📌 Structured Query Language (SQL): Standard language for managing and querying relational databases.
- 📌 Database: Collection of related tables storing data.
8.2 STRUCTURED QUERY LANGUAGE (SQL)
Explanation8.2 STRUCTURED QUERY LANGUAGE (SQL)
Structured Query Language (SQL) is a special programming language designed for managing and manipulating data in relational database management systems (RDBMS). Unlike file systems where application programs are needed to access data, databases use query languages like SQL to access and manipulate data efficiently. SQL is widely used by major RDBMS software such as MySQL, Oracle, and SQL Server. It is easy to learn because its statements use descriptive English words and are not case sensitive. SQL allows users to specify what data to retrieve without needing to specify how to retrieve it, making data access simpler and more efficient. Besides querying, SQL provides commands for defining database structures, manipulating data, declaring constraints, and retrieving data in various ways. In this chapter, MySQL is used as the RDBMS software to demonstrate SQL commands. We will create a database named StudentAttendance, populate it with data, manipulate the data, and retrieve data using SQL queries. Installing MySQL involves downloading it from the official website and starting the MySQL service. The mysql> prompt indicates readiness to accept SQL commands. Important rules for writing SQL statements include that SQL is case insensitive, statements must end with a semicolon (;), and multiline statements can be continued by pressing enter without a semicolon until the last line, where a semicolon is required. This section also includes an activity to explore LibreOffice Base and compare it with MySQL to understand different database management tools.
- SQL is a query language used to interact with RDBMS.
- SQL statements are easy to learn and not case sensitive.
- SQL specifies what data to retrieve, not how to retrieve it.
- MySQL is an open-source RDBMS used to demonstrate SQL.
- SQL statements end with a semicolon (;).
- Multiline SQL statements continue with prompt changing to '->'.
- 📌 SQL (Structured Query Language): Language to manage and query relational databases.
- 📌 MySQL: Open-source RDBMS software.
- 📌 Query: SQL command to retrieve or manipulate data.
8.3 DATA TYPES AND CONSTRAINTS IN MySQL
Explanation8.3 DATA TYPES AND CONSTRAINTS IN MySQL
A database consists of one or more relations (tables), each made up of attributes (columns). Each attribute has a data type that defines the kind of data it can hold and the operations that can be performed on it. For example, arithmetic operations c
Practice Questions — Introduction to Chapter Structured Query 8 Language (SQL)
Includes NCERT exercise questions with answers
Q1.Match the following clauses with their respective functions. ALTER - Insert the values in a table UPDATE - Restrictions on columns DELETE - Table definition INSERT INTO - Change the name of a column CONSTRAINTS - Update existing information in a table DESC - Delete an existing row from a table CREATE - Create a database
Answer:
Correct matching: ALTER - Change the name of a column UPDATE - Update existing information in a table DELETE - Delete an existing row from a table INSERT INTO - Insert the values in a table CONSTRAINTS - Restrictions on columns DESC - Table definition CREATE - Create a database Explanation: - ALTER is used to modify table structure such as changing column names. - UPDATE modifies existing data in a table. - DELETE removes rows from a table. - INSERT INTO adds new rows. - CONSTRAINTS define rules on columns. - DESC shows table structure (description). - CREATE is used to create database or tables.
Explanation:
Step-by-step: 1. ALTER is for altering table structure, e.g., changing column name. 2. UPDATE modifies existing data. 3. DELETE removes rows. 4. INSERT INTO adds rows. 5. CONSTRAINTS define restrictions on columns. 6. DESC describes table structure. 7. CREATE creates database or tables.
Q2.Choose appropriate answer with respect to the following code snippet. CREATE TABLE student ( name CHAR(30), student_id INT, gender CHAR(1), PRIMARY KEY (student_id) ); a) What will be the degree of student table? i) 30 ii) 1 iii) 3 iv) 4 b) What does ‘name’ represent in the above code snippet? i) a table ii) a row iii) a column iv) a database c) What is true about the following SQL statement? Select * FROM student; i) Displays contents of table ‘student’ ii) Displays column names and contents of table ‘student’ iii) Results in error as improper case has been used iv) Displays only the column names of table ‘student’ d) What will be the output of following query? INSERT INTO student VALUES (“Suhana”,109, ^F*), VALUES (“Rivaan”,102, ^M*), VALUES (“Athary”,103, ^M*), VALUES (“Rishika”,105, ^F*), VALUES (“Garvit”,104, ^M*), VALUES (“Shaurya”,109, ^M*); i) Error ii) No Error iii) Depends on compiler iv) Successful completion of the query e) In the following query how many rows will be deleted? DELETE student WHERE student_id=109; i) 1 row ii) All the rows where student ID is equal to 109 iii) No row will be deleted iv) 2 rows
Answer:
Degree of student table is the number of columns. Columns are name, student_id, gender = 3 columns. So answer is iii) 3. b) 'name' is a column in the table student. So answer is iii) a column. c) Select * FROM student; displays all columns and all rows of the student table. So answer is ii) Displays column names and contents of table ‘student’. d) The INSERT INTO query syntax is incorrect because VALUES keyword should be used once with multiple rows separated by commas, not repeated. Also, the gender values are shown as ^F* and ^M* which are invalid. So answer is i) Error. e) DELETE student WHERE student_id=109; is syntactically incorrect because DELETE requires FROM keyword: DELETE FROM student WHERE student_id=109; Assuming corrected syntax, it will delete all rows where student_id=109. Since student_id is primary key, only one row can have this value. So answer is i) 1 row.
Explanation:
Step-by-step: (a) Degree = number of columns = 3. (b) 'name' is a column name. (c) SELECT * displays all columns and rows. (d) INSERT syntax is wrong due to repeated VALUES and invalid gender values. (e) DELETE without FROM is wrong; corrected query deletes rows with student_id=109; only one row possible due to primary key.
Q3.Fill in the blanks: a) __________ declares that an index in one table is related to that in another table. i) Primary Key ii) Foreign Key iii) Composite Key iv) Secondary Key b) The symbol Asterisk (*) in a select query retrieves i) All data from the table ii) Data of primary key only iii) NULL data iv) None of the mentioned
Answer:
a) Foreign Key declares that an index in one table is related to that in another table. b) The symbol Asterisk (*) in a select query retrieves all data from the table.
Explanation:
a) Foreign Key is used to link two tables by referencing primary key of another table. b) Asterisk (*) in SELECT * retrieves all columns and all rows from the table.
Q4.Consider the following MOVIE database and answer the SQL queries based on it. MovieID | MovieName | Category | ReleaseDate | ProductionCost | BusinessCost 001 | Hindi_Movie | Musical | 2018-04-23 | 124500 | 130000 002 | Tamil_Movie | Action | 2016-05-17 | 112000 | 118000 003 | English_Movie | Horror | 2017-08-06 | 245000 | 360000 004 | Bengali_Movie | Adventure | 2017-01-04 | 72000 | 100000 005 | Telugu_Movie | Action | - | 100000 | - 006 | Punjabi_Movie | Comedy | - | 30500 | - a) Retrieve movies information without mentioning their column names. b) List business done by the movies showing only MovieID, MovieName and BusinessCost. c) List the different categories of movies. d) Find the net profit of each movie showing its ID, Name and Net Profit. (Hint: Net Profit = BusinessCost - ProductionCost) Make sure that the new column name is labelled as NetProfit. Is this column now a part of the MOVIE relation? If no, then what name is coined for such columns? What can you say about the profit of a movie which has not yet released? Does your query result show profit as zero? e) List all movies with ProductionCost greater than 80,000 and less than 1,25,000 showing ID, Name and ProductionCost. f) List all movies which fall in the category of Comedy or Action. g) List the movies which have not been released yet.
Answer:
a) SQL Query: SELECT * FROM MOVIE; b) SQL Query: SELECT MovieID, MovieName, BusinessCost FROM MOVIE; c) SQL Query: SELECT DISTINCT Category FROM MOVIE; d) SQL Query: SELECT MovieID, MovieName, (BusinessCost - ProductionCost) AS NetProfit FROM MOVIE; Explanation: - The new column NetProfit is a derived column (alias), not stored in the table permanently. - Such columns are called computed or derived columns. - For movies not yet released (BusinessCost or ReleaseDate missing), profit calculation may result in NULL or zero depending on data. - Query result will show NULL or no profit for unreleased movies. e) SQL Query: SELECT MovieID, MovieName, ProductionCost FROM MOVIE WHERE ProductionCost > 80000 AND ProductionCost < 125000; f) SQL Query: SELECT * FROM MOVIE WHERE Category IN ('Comedy', 'Action'); g) SQL Query: SELECT * FROM MOVIE WHERE ReleaseDate IS NULL OR ReleaseDate = '-';
Explanation:
Step-by-step: a) SELECT * retrieves all columns and rows. b) Select only specified columns. c) DISTINCT gets unique categories. d) Use arithmetic operation with alias for NetProfit. e) Use WHERE with ProductionCost range. f) Use WHERE with IN clause for categories. g) Use WHERE with NULL or placeholder for unreleased movies.
Q5.Suppose your school management has decided to conduct cricket matches between students of class XI and Class XII. Students of each class are asked to join any one of the four teams — Team Titan, Team Rockers, Team Magnet and Team Hurricane. During summer vacations, various matches will be conducted between these teams. Help your sports teacher to do the following: a) Create a database "Sports". b) Create a table "TEAM" with following considerations: i) It should have a column TeamID for storing an integer value between 1 to 9, which refers to unique identification of a team. ii) Each TeamID should have its associated name (TeamName), which should be a string of length not less than 10 characters. c) Using table level constraint, make TeamID as primary key. d) Show the structure of the table TEAM using SQL command. e) As per the preferences of the students four teams were formed as given below. Insert these four rows in TEAM table: Row 1: (1, Team Titan) Row 2: (2, Team Rockers) Row 3: (3, Team Magnet) Row 4: (4, Team Hurricane) f) Show the contents of the table TEAM. g) Now create another table below. MATCH_DETAILS and insert data as shown in table. Choose appropriate domains and constraints for each attribute. Table: MATCH_DETAILS MatchID | MatchDate | FirstTeamID | SecondTeamID | FirstTeamScore | SecondTeamScore M1 | 2018-07-17 | 1 | 2 | 90 | 86 M2 | 2018-07-18 | 3 | 4 | 45 | 48 M3 | 2018-07-19 | 1 | 3 | 78 | 56 M4 | 2018-07-19 | 2 | 4 | 56 | 67 M5 | 2018-07-20 | 1 | 4 | 32 | 87 M6 | 2018-07-21 | 2 | 3 | 67 | 51 h) Use the foreign key constraint in the MATCH_DETAILS table with reference to TEAM table so that MATCH_DETAILS table records score of teams existing in the TEAM table.
Answer:
a) CREATE DATABASE Sports; b) CREATE TABLE TEAM ( TeamID INT CHECK (TeamID BETWEEN 1 AND 9), TeamName VARCHAR(50) CHECK (LENGTH(TeamName) >= 10) ); c) ALTER TABLE TEAM ADD CONSTRAINT pk_TeamID PRIMARY KEY (TeamID); Alternatively, define primary key in CREATE TABLE: CREATE TABLE TEAM ( TeamID INT PRIMARY KEY CHECK (TeamID BETWEEN 1 AND 9), TeamName VARCHAR(50) CHECK (LENGTH(TeamName) >= 10) ); d) To show structure: DESC TEAM; or SHOW COLUMNS FROM TEAM; e) Insert rows: INSERT INTO TEAM VALUES (1, 'Team Titan'); INSERT INTO TEAM VALUES (2, 'Team Rockers'); INSERT INTO TEAM VALUES (3, 'Team Magnet'); INSERT INTO TEAM VALUES (4, 'Team Hurricane'); f) Show contents: SELECT * FROM TEAM; g) Create MATCH_DETAILS table: CREATE TABLE MATCH_DETAILS ( MatchID VARCHAR(5) PRIMARY KEY, MatchDate DATE, FirstTeamID INT, SecondTeamID INT, FirstTeamScore INT, SecondTeamScore INT, CONSTRAINT fk_FirstTeam FOREIGN KEY (FirstTeamID) REFERENCES TEAM(TeamID), CONSTRAINT fk_SecondTeam FOREIGN KEY (SecondTeamID) REFERENCES TEAM(TeamID) ); Insert data: INSERT INTO MATCH_DETAILS VALUES ('M1', '2018-07-17', 1, 2, 90, 86); INSERT INTO MATCH_DETAILS VALUES ('M2', '2018-07-18', 3, 4, 45, 48); INSERT INTO MATCH_DETAILS VALUES ('M3', '2018-07-19', 1, 3, 78, 56); INSERT INTO MATCH_DETAILS VALUES ('M4', '2018-07-19', 2, 4, 56, 67); INSERT INTO MATCH_DETAILS VALUES ('M5', '2018-07-20', 1, 4, 32, 87); INSERT INTO MATCH_DETAILS VALUES ('M6', '2018-07-21', 2, 3, 67, 51); h) Foreign key constraints are added in MATCH_DETAILS table creation as shown above to ensure FirstTeamID and SecondTeamID values exist in TEAM table. Explanation: - Database creation is straightforward. - Table TEAM has constraints on TeamID and TeamName length. - Primary key constraint ensures uniqueness. - Foreign key constraints maintain referential integrity between MATCH_DETAILS and TEAM. - Data insertion follows the table structure.
Explanation:
Step-by-step: 1. Create database. 2. Create TEAM table with constraints. 3. Add primary key. 4. Show structure using DESC. 5. Insert team data. 6. Show contents. 7. Create MATCH_DETAILS with appropriate data types and foreign keys. 8. Insert match data. 9. Foreign keys ensure team IDs in MATCH_DETAILS exist in TEAM.
Q6.Which of the following best describes Structured Query Language (SQL)?
Answer:
A standard language for managing and manipulating relational databases
Explanation:
SQL is a standard programming language specifically designed for managing and manipulating relational databases. It allows users to create, retrieve, update, and delete data stored in tables within a database.
Q7.Which category of SQL commands is used to define and modify the structure of database objects such as tables and indexes?
Answer:
Data Definition Language (DDL)
Explanation:
DDL commands like CREATE, ALTER, and DROP are used to define and modify the structure of database objects such as tables, indexes, and schemas.
Q8.Identify the SQL command used to permanently delete an existing table from a database.
Answer:
DROP TABLE
Explanation:
The DROP TABLE command deletes an existing table permanently from the database, removing both its structure and data.
All 8 Chapters in Informatics Practices
Informatics Practices · Class 11