0% found this document useful (0 votes)
9 views2 pages

SQL & Python Exam Study Guide

The document provides comprehensive answers to SQL and Python exam questions, covering string and aggregate functions, table creation, and operations in SQL, as well as file handling in Python. It also includes important midterm questions on stacks, SQL commands, exception handling, and data types. The content is structured into 2M and 3M questions, detailing various programming concepts and database management techniques.

Uploaded by

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

SQL & Python Exam Study Guide

The document provides comprehensive answers to SQL and Python exam questions, covering string and aggregate functions, table creation, and operations in SQL, as well as file handling in Python. It also includes important midterm questions on stacks, SQL commands, exception handling, and data types. The content is structured into 2M and 3M questions, detailing various programming concepts and database management techniques.

Uploaded by

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

SQL & Python Exam Answers

5M Questions

1. Explain String function in SQL (M=5)


- String functions are used to perform operations on string data types. Examples:
• UPPER() → Converts string to uppercase.
• LOWER() → Converts string to lowercase.
• SUBSTRING(str, start, length) → Extracts part of a string.
• CONCAT(str1, str2) → Joins two strings.
• LENGTH(str) → Returns string length.

2. Explain Aggregate function in SQL (M=5)


- Aggregate functions perform calculations on multiple rows and return a single result.
• COUNT() → Counts number of rows.
• SUM() → Adds numeric values.
• AVG() → Returns average value.
• MIN() → Returns smallest value.
• MAX() → Returns largest value.

3. SQL Table Creation & Operations (M=5)


• CREATE TABLE Student (
Rollno INT,
Sname VARCHAR(15),
DOB DATE
);

• INSERT INTO Student VALUES (1, 'Arun', '2003-02-12');


INSERT INTO Student VALUES (2, 'Bina', '2002-06-20');
INSERT INTO Student VALUES (3, 'Ravi', '2003-11-05');

• ALTER TABLE Student ADD Gender CHAR(1);


ALTER TABLE Student ADD Comb VARCHAR(5);

• DESC Student; → shows structure.

• SELECT * FROM Student; → lists all records.

4. Employee Database (M=5)


• CREATE DATABASE employee;
• CREATE TABLE employee (
Empno INT,
Ename VARCHAR(15),
Salary INT
);

• INSERT INTO employee VALUES (101, 'John', 25000);


INSERT INTO employee VALUES (102, 'Sara', 30000);
INSERT INTO employee VALUES (103, 'Amit', 28000);

• ALTER TABLE employee ADD Address VARCHAR(25);


• DROP TABLE employee;
5. Python program for reading/writing binary file (M=5)

with open("[Link]", "wb") as f:


[Link](b"Hello World")

with open("[Link]", "rb") as f:


content = [Link]()
print(content)

---------------------------------------------------
Midterm Important Questions

2M Questions
1. Applications of stack: Expression evaluation, function call management.
2. Function for isEmpty(), push(), pop() in stack.
3. Two notations: Infix (A+B), Postfix (AB+).
4. Expand SQL: Structured Query Language. Example RDBMS: MySQL, Oracle.
5. DDL commands: CREATE, ALTER, DROP.
6. DML commands: INSERT, UPDATE, DELETE, SELECT.
7. DROP TABLE syntax: DROP TABLE table_name;
8. DELETE command: DELETE FROM table_name WHERE condition;

3M Questions
1. Raising statement: raise Exception("error").
2. Assert statement: assert x>0, "must be positive".
3. Need of exception handling: Prevents program crash, ensures error control.
4. TRY/EXCEPT/FINALLY: try block executes, except handles error, finally always runs.
5. Data collection types: Structured (tables), Unstructured (images/text).
6. CREATE TABLE syntax example.
7. INSERT INTO syntax example.
8. Date functions: CURDATE(), NOW(), DATE_ADD().

5M Questions
1. Built-in expressions: Arithmetic, comparison, logical, bitwise, string.
2. Catching exception example using try-except.
3. File types: Text vs Binary (human-readable vs machine-readable).
4. File open modes: R, W, A, R+, W+, etc.
5. Stack operations: push, pop, peek, size.
6. Mean, median, mode, range, std. deviation definitions.
7. Data types in MySQL: CHAR, VARCHAR, TEXT, INT, FLOAT, DATE.
8. Constraints in MySQL: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, DEFAULT.
9. ALTER statements:
- Add primary key → ALTER TABLE t ADD PRIMARY KEY (col);
- Add column → ALTER TABLE t ADD col datatype;
- Drop → ALTER TABLE t DROP COLUMN col;

Common questions

Powered by AI

Built-in expressions in programming offer fundamental functionality for diverse computing needs. Arithmetic expressions perform mathematical calculations, e.g., 5 + 3. Comparison expressions evaluate relationships between values, e.g., 5 > 3. Logical expressions, such as AND and OR, combine multiple conditions, e.g., (x > 5) AND (y < 10). Bitwise expressions manipulate data at the bit level, e.g., 5 & 3. Finally, string expressions concatenate or modify strings, e.g., 'Hello' + ' ' + 'World'. These expressions facilitate complex algorithms and control structures across various programming domains .

Aggregate functions in SQL perform calculations on multiple rows of a table's column(s) and return a single result. Common aggregate functions include COUNT(), which counts the number of rows; SUM(), which adds up numeric values; AVG(), which calculates the average value; MIN(), which returns the smallest value; and MAX(), which returns the largest value .

In Python, file operations can be performed in either text mode or binary mode. Text mode (default) reads and writes files as text, suitable for human-readable content, while binary mode deals with binary files, which are more efficient for machine-readable data. For example, using 'r' or 'w' opens a file in text mode for reading or writing, respectively, whereas 'rb' or 'wb' opens a file in binary mode for reading or writing. An example in binary mode is with open('data.bin', 'wb') to write bytes to a file and open('data.bin', 'rb') to read those bytes .

The DROP TABLE command in SQL is used to delete a table entirely from the database, including all its structure, data, indexes, and permissions. This operation is irreversible and should be used with caution. For example, DROP TABLE employee will remove the 'employee' table from the database if it is no longer needed or if its archived data is transferred elsewhere. It's typically used during database cleanup or when restructuring is necessary .

String functions in SQL are used to perform various operations on string data types. These include transforming strings, extracting substrings, and measuring string lengths. Examples include UPPER() which converts a string to uppercase, LOWER() which converts it to lowercase, SUBSTRING(str, start, length) which extracts part of a string, CONCAT(str1, str2) which joins two strings, and LENGTH(str) which returns the length of a string .

Stacks in programming play a vital role in managing function calls and expression evaluations due to their LIFO (Last In, First Out) structure. They help in managing function calls through the call stack by keeping track of active subroutines, enabling the program to return to the correct execution point after a function completes. In expression evaluations, stacks assist in converting infix expressions (e.g., A+B) into postfix (e.g., AB+) for easier computation by algorithmic processes .

Primary key constraints in MySQL uniquely identify each row in a table, ensuring that no duplicate values exist in the specified column(s) and that the values are not NULL, thereby maintaining entity integrity. A foreign key constraint links two tables, ensuring referential integrity by enforcing relationships between tables. It requires that the value in a column, or set of columns, matches a value in the primary key of another table, preventing invalid data entry .

DDL (Data Definition Language) commands in SQL are used to define and manage all database objects, such as tables and indexes, without manipulating data within them. Examples include CREATE, ALTER, and DROP. For instance, CREATE TABLE defines a new table's structure. DML (Data Manipulation Language) commands, on the other hand, are used to manipulate data stored in database objects. This includes INSERT (to add new records), UPDATE (to modify existing records), and DELETE (to remove records). SELECT is used to query and retrieve data .

Creating a table in SQL involves defining its structure with the CREATE TABLE statement, specifying column names and data types. For example, CREATE TABLE Student (Rollno INT, Sname VARCHAR(15), DOB DATE) defines a table with student roll numbers, names, and dates of birth. Modifying a table can include adding columns or altering data types using the ALTER TABLE command. For instance, ALTER TABLE Student ADD Gender CHAR(1) adds a gender column. Reviewing the structure can be done with DESC Student, and viewing all records with SELECT * FROM Student .

Exception handling is crucial in programming as it prevents program crashes and provides mechanisms to handle unexpected errors gracefully, ensuring robustness and security. A try-except-finally block assists in managing these exceptions: the try block contains code that might throw an error, the except block captures and handles the error to prevent program interruption, and the finally block contains cleanup code that executes regardless of an error occurrence, maintaining program order .

You might also like