0% found this document useful (0 votes)
15 views5 pages

SQL Test 5 - Copy (2)

The document is an SQL test for freshers, comprising theory questions on SQL concepts such as the difference between SQL and MySQL, foreign keys, ACID properties, views, and stored procedures. It also includes practical SQL queries for tasks like retrieving employee salaries, modifying tables, and joining data from multiple tables, as well as aptitude questions related to database operations and calculations. Overall, it serves as a comprehensive assessment tool for evaluating foundational SQL knowledge and skills.

Uploaded by

Abhishek Shete
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
15 views5 pages

SQL Test 5 - Copy (2)

The document is an SQL test for freshers, comprising theory questions on SQL concepts such as the difference between SQL and MySQL, foreign keys, ACID properties, views, and stored procedures. It also includes practical SQL queries for tasks like retrieving employee salaries, modifying tables, and joining data from multiple tables, as well as aptitude questions related to database operations and calculations. Overall, it serves as a comprehensive assessment tool for evaluating foundational SQL knowledge and skills.

Uploaded by

Abhishek Shete
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

SQL Test for Freshers

Theory Questions
1. What is the difference between SQL and MySQL?
Ans-
 SQL (Structured Query Language) is a standard language used
for managing and manipulating relational databases. It is used to
query, update, insert, and delete data, as well as to define and
manage database schemas. SQL is a language that can be used
with any relational database system (e.g., MySQL, PostgreSQL,
Oracle).
 MySQL is a specific relational database management system
(RDBMS) that uses SQL as its query language. It is open-source,
widely used for web applications, and supports various features
such as data storage, retrieval, and transaction management.
Key Difference:
 SQL is a query language.
 MySQL is an RDBMS that implements SQL.

2. Explain what a foreign key is and how it is used in relational


databases.
Ans-
A foreign key is a column or a set of columns in one table that links to
the primary key of another table. The foreign key establishes a
relationship between two tables and enforces referential integrity,
meaning it ensures that a record in one table corresponds to a valid
record in another table.
 Purpose: Ensures consistency and integrity of the data between
related tables.
 Usage: A foreign key in a child table references a primary key in a
parent table.
For example, in a database with Orders and Customers tables, the
Orders table might have a CustomerID as a foreign key, which
references the CustomerID in the Customers table. This ensures that
each order is linked to a valid customer.

3. What are the ACID properties in database transactions?


Ans-
The ACID properties ensure that database transactions are processed
reliably and protect the integrity of the database. These properties are:
 Atomicity: A transaction is treated as a single unit, meaning it
either completes entirely or not at all. If one part of the transaction
fails, the entire transaction is rolled back.
 Consistency: A transaction must bring the database from one valid
state to another valid state. The database must always meet the
defined rules, constraints, and integrity checks.
 Isolation: Transactions are isolated from each other, meaning the
changes made by one transaction are not visible to others until the
transaction is committed. This prevents data corruption from
concurrent transactions.
SQL Test for Freshers

 Durability: Once a transaction is committed, its changes are


permanent, even in the event of a system crash or power failure.

4. Define a view in SQL and explain its advantages.


Ans-
A view in SQL is a virtual table that represents the result of a stored
query. It is a predefined query that you can treat like a table for the
purpose of selecting data. A view does not store data itself but
presents data from one or more underlying tables.
Advantages of Views:
 Simplified Queries: Views simplify complex queries by
encapsulating them in a single view, making data retrieval easier for
users.
 Security: Views can restrict access to certain columns or rows by
only showing a subset of the data, thus improving security.
 Data Abstraction: Views abstract the underlying table structure,
making it easier to modify the schema without affecting
applications.
 Reusability: Once created, views can be reused in multiple queries,
reducing redundancy.

5. What is a stored procedure, and when would you use one?


Ans-
A stored procedure is a set of precompiled SQL statements that can be
executed as a single unit. Stored procedures are stored in the database
and can be called by applications or users to perform specific tasks, such
as inserting, updating, or deleting data.
When to use a stored procedure:
 Code Reusability: To encapsulate and reuse business logic across
multiple applications or queries.
 Performance: Stored procedures are precompiled, which can lead to
improved performance compared to sending individual SQL queries.
 Security: By using stored procedures, you can restrict access to data
and prevent SQL injection attacks.
 Transaction Management: Stored procedures can handle complex
transactions, ensuring that multiple operations are completed
successfully or rolled back if an error occurs.
Stored procedures are ideal for tasks like batch processing, complex
calculations, and routine database operations.

Practical Questions
1. Write a SQL query to retrieve the top 5 highest-paid
employees from the "Employees" table.
Ans-
SELECT *
FROM Employees
ORDER BY Salary DESC
LIMIT 5;
SQL Test for Freshers

2. Create a SQL statement to add a new column called


"PhoneNumber" (VARCHAR) to the "Customers" table.
Ans-
ALTER TABLE Customers
ADD COLUMN PhoneNumber VARCHAR(15);
3. Write a query to retrieve the average salary of employees in
the "Employees" table.
Ans-
SELECT AVG(Salary) AS AverageSalary
FROM Employees;
4. Write a SQL query to find all products that have a price
greater than $100 from the "Products" table.
Ans-
SELECT *
FROM Products
WHERE Price > 100;
5. Write a query to select all orders from the "Orders" table
where the order date is in the last 30 days.
Ans-
SELECT *
FROM Orders
WHERE OrderDate >= CURRENT_DATE - INTERVAL 30 DAY;
6. Create a SQL query to join the "Customers" and "Orders"
tables to show customer names along with their order
details.
Ans-
SELECT c.CustomerName, o.OrderID, o.OrderDate, o.TotalAmount
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID;
7. Write a query to find duplicate entries in a "Users" table
based on the "Email" column.
Ans-
SELECT Email, COUNT(*) AS DuplicateCount
FROM Users
GROUP BY Email
HAVING COUNT(*) > 1;
8. Create a SQL statement to delete all records from the
"Sessions" table where the session date is older than one
year.
Ans-
DELETE FROM Sessions
WHERE SessionDate < CURRENT_DATE - INTERVAL 1 YEAR;
9. Write a SQL query to find the second highest salary from the
"Employees" table.
Ans-
SELECT MAX(Salary) AS SecondHighestSalary
FROM Employees
WHERE Salary < (SELECT MAX(Salary) FROM Employees);
SQL Test for Freshers

10. Write a SQL query to create a new table called


"Inventory" with the following columns: ItemID (INT),
ItemName (VARCHAR), Quantity (INT), Price (DECIMAL).
Ans-
CREATE TABLE Inventory (
ItemID INT,
ItemName VARCHAR(255),
Quantity INT,
Price DECIMAL(10, 2)
);

Aptitude Questions
1. If a database has 4 tables and each table has an average of
25 records, what is the total number of records across all
tables?
Ans-
If a database has 4 tables and each table has an average of 25 records,
what is the total number of records across all tables?
 Total records = 4 tables × 25 records per table = 100 records.

2. If a query retrieves 20 records, and you want to double that,


how many additional records do you need to retrieve?
Ans-
If a query retrieves 20 records, and you want to double that, how
many additional records do you need to retrieve?
 To double the records from 20 to 40, you need an additional 20
records.

3. A database operation takes 10 minutes to run. If the


complexity doubles, how long might it take?
Ans-
A database operation takes 10 minutes to run. If the complexity
doubles, how long might it take?
 If the complexity doubles, the time to execute the operation will
likely double as well.
 New time = 10 minutes × 2 = 20 minutes.

4. If an employee earns $50 per hour and works 40 hours a


week, what is their weekly salary?
Ans-
If an employee earns $50 per hour and works 40 hours a week, what is
their weekly salary?
 Weekly salary = $50/hour × 40 hours/week = $2,000.

5. A database can store 1,000,000 records. If each record


requires 250 bytes, what is the total storage space required
in megabytes?
Ans-
SQL Test for Freshers

A database can store 1,000,000 records. If each record requires 250


bytes, what is the total storage space required in megabytes?
 Total storage space = 1,000,000 records × 250 bytes = 250,000,000
bytes.
 To convert bytes to megabytes:
250,000,000 bytes÷1,048,576 bytes per MB≈238.42 MB250,000,000
\, \text{bytes} ÷ 1,048,576 \, \text{bytes per MB} \approx
238.42 \, \
text{MB}250,000,000bytes÷1,048,576bytes per MB≈238.42MB.
 The total storage space required is approximately 238.42 MB.

You might also like