Computer Science Exam Questions
Computer Science Exam Questions
Relational algebra is a procedural query language used to query databases in various ways. It forms the theoretical foundation for SQL but operates on a more abstract level using operators like SELECT (σ), PROJECT (π), and JOIN (⨝). For instance, in the Teach relation (Name, Address, Course), the query to find teachers teaching 'DBMS' can be expressed as σ_Course='DBMS'(Teach). To project names and addresses of those teaching 'computer', the expression would be π_Name, Address(σ_Course='computer'(Teach)). These operations allow us to manipulate and retrieve structured data, showcasing precise control over how data is selected and presented .
To design an SQL statement to list all teachers living in 'Mumbai' from the Teach relation, you use the SELECT statement alongside a WHERE clause: SELECT Name FROM Teach WHERE Address='Mumbai'; The considerations include ensuring the correct case sensitivity of string literals, ensuring there are no leading or trailing spaces in the data stored within the column, and checking that the column 'Address' is indexed for faster retrieval if the dataset is large .
To extract specific insights using SQL, you can write queries that leverage the SELECT statement along with JOINs and WHERE conditions. Here are a few examples using the APPLICANTS and COURSES tables: 1. To display applicants who joined before 2010: SELECT NAME, FEE, Gender, JOINYEAR FROM APPLICANTS WHERE JOINYEAR < 2010; 2. To display the names of applicants paying a fee of more than 30000: SELECT NAME FROM APPLICANTS WHERE FEE > 30000; 3. To join and display the applicants' names with their course names: SELECT A.NAME, C.COURSES FROM APPLICANTS A, COURSES C WHERE A.C_ID = C.C_ID; These queries illustrate the ability to filter, aggregate, and cross-reference data across tables, providing insights into the nature and relationships of the data .
You would construct the SQL query using the SELECT statement along with MAX function to find the name of the programmer. The query would be: SELECT P_Name FROM PROGRAMMERS WHERE SAL = (SELECT MAX(SAL) FROM PROGRAMMERS); This query first determines the maximum salary using a subquery, and then retrieves the programmer's name associated with that salary .
Primary keys and alternate keys both serve to uniquely identify records in a table but differ in their naming and contextual use. A primary key is the main field selected to uniquely identify records, ensuring no duplication. For example, a 'CustomerID' in a customer table is typically a primary key. An alternate key is any candidate key that is not chosen as the primary key. For instance, an 'Email' column that also uniquely identifies customers could be an alternate key. The distinction lies in usage: the primary key is selected as the main identifier for querying and operations, while alternate keys offer other potential unique identifiers .
GROUP BY and ORDER BY clauses serve different purposes in SQL: GROUP BY is used to aggregate data across specified columns, often used with functions like COUNT, AVG, SUM, etc., whereas ORDER BY sorts the results of a query in a specified order, either ascending or descending. Example of GROUP BY: To count employees in each department: SELECT DEPARTMENT, COUNT(*) FROM EMPLOYEE GROUP BY DEPARTMENT; This categorizes employees into departments and counts them. Example of ORDER BY: To sort employees by name: SELECT * FROM EMPLOYEE ORDER BY NAME; This arranges all records by employee names alphabetically. While GROUP BY impacts the structure of the data, ORDER BY affects its presentation .
DDL (Data Definition Language) and DML (Data Manipulation Language) are integral components of SQL utilized within database management. DDL pertains to the schema and defines structures, allowing the creation and modification of database objects such as tables and indexes. On the other hand, DML is used for data manipulation, enabling the insertion, updating, deletion, and retrieval of data within these tables. While DDL sets the framework within which databases operate, DML allows for the practical management and manipulation of the data held within this framework. Together, they facilitate the holistic management of databases, with DDL setting up the necessary structures and constraints and DML enabling the everyday operations on data .
SQL functions like MAX, MIN, and AVG enhance data query capabilities by allowing aggregation over data to derive insights. MAX returns the largest value in a set, MIN the smallest, and AVG the average. For example, to find the maximum salary from the EMPLOYEE table, the query is SELECT MAX(BASIC) FROM EMPLOYEE; to find the minimum joining year from APPLICANTS based on conditions, the query is SELECT MIN(JOINYEAR) FROM APPLICANTS WHERE GENDER='M'; and to calculate the average basic salary of women employees, the query is SELECT AVG(BASIC) FROM EMPLOYEE WHERE SEX='F'; These functions enable comprehensive data analysis by providing statistical summaries that aid in decision-making .
A Cartesian product in SQL is formed when every row of one table is combined with every row of another table, resulting in a number of rows that equals the product of the number of rows in the first and second tables. It is achieved using a simple SELECT statement without specifying any join condition (i.e., without a WHERE clause that defines how the tables relate). For example, if we have tables 'Students' and 'Courses', running SELECT * FROM Students, Courses; will yield a Cartesian product. The implication of a Cartesian product is that it can lead to very large result sets, which are often unintentional and inefficient. It is primarily useful for situations where all possible combinations of the tables need to be considered, such as certain analytical tasks or testing purposes .
Candidate keys in a database are essential for uniquely identifying tuples within a table. A candidate key is a minimal set of attributes that can uniquely identify a record. It is important because it ensures that each record in the table can be uniquely retrieved. For example, in a table with columns 'StudentID', 'Name', and 'Email', both 'StudentID' and 'Email' could serve as candidate keys, assuming they uniquely identify the records. The importance of candidate keys lies in ensuring data integrity and enforcing unique records, which is crucial for database normalization and design .