9 - SQL Notes
9 - SQL Notes
https://drive.google.com/file/d/1uph1pC2IONe-vgVCesXAglyAYyBRbNTg/view?usp=sharing
SELECT * FROM
table_name;
Use the UNION operator to combine results from multiple SELECT statements.
Use aggregate functions (SUM, AVG, COUNT, MAX, MIN) for data analysis.
Ans: To find the nth highest salary in a SQL table, you can use the following SQL query.
Replace n with the desired position of the highest salary you want to retrieve:
FROM Employee
In this query, Employee is the name of the table, and Salary is the name of the column
containing the salary information. The ORDER BY Salary DESC sorts the salaries in
descending order (highest to lowest), and the LIMIT 1 OFFSET n-1 part retrieves the nth
highest salary. Make sure to replace Employee and Salary with the actual table and column
names in your database.
1. Write An SQL Query To Print Details Of The EMPLOYEEs Whose FIRST_NAME Ends
With ‘A’.
2. Write An SQL Query To Print Details Of The EMPLOYEEs Whose FIRST_NAME Ends
With ‘H’ And Contains Six Alphabets.
3. Write An SQL Query To Print Details Of The EMPLOYEEs Whose SALARY Lies
Between 100000 And 500000.
4. Write An SQL Query To Print Details Of The EMPLOYEEs Who Have Joined In
Feb’2014.
Select * from EMPLOYEE where year(JOINING_DATE) = 2014 and
month(JOINING_DATE) = 2
5. Write An SQL Query To Fetch EMPLOYEE Names With Salaries >= 50000 And <=
100000.
6. Write An SQL Query To Fetch The No. Of EMPLOYEEs For Each Department In The
Descending Order.
7. Write An SQL Query To Print Details Of The EMPLOYEEs Who Are Also Managers.
SELECT
EMPLOYEE.EMPLOYEE_ID,EMPLOYEE.DEPARTMENT,EMPLOYEE.FIRST_NAME ,Title.E
MPLOYEE_TITLE Title from EMPLOYEE Inner Join Title
on EMPLOYEE.EMPLOYEE_ID=Title.EMPLOYEE_Ref_ID where
Title.EMPLOYEE_TITLE='Manager' order by EMPLOYEE_ID
8. Write An SQL Query To Fetch Duplicate Records Having Matching Data In Some Fields
Of A Table.
10. Write An SQL Query To Show Only Even Rows From A Table.
In SQL, "DROP," "TRUNCATE," and "DELETE" are three different commands used for different
purposes related to managing data and database objects. Here's a brief explanation of each:
1. DROP:
o Purpose: The DROP command is used to delete database objects, such as
tables, indexes, or views. When you drop a table, all the data within the table is
permanently removed, and the table itself no longer exists in the database.
o Effect: Irreversibly deletes the entire table structure along with its data. It cannot
be rolled back.
2. Example:
l
TRUNCATE:
Purpose: The TRUNCATE command is used to quickly remove all rows from a table
without logging individual row deletions. It's a faster operation compared to DELETE
because it doesn't generate as much transaction log data.
Effect: Removes all rows from a table, but the table structure remains intact.
Example:
DELETE:
Purpose: The DELETE command is used to remove specific rows from a table based on
a condition or criteria. It is more flexible than TRUNCATE because you can specify
which rows to delete.
Effect: Removes selected rows from a table, and you can use a WHERE clause to
specify the conditions for deletion. It generates transaction log entries for each deleted
row.
Example:
DROP deletes the entire table structure and data, and it's used for removing entire
database objects.
TRUNCATE removes all rows from a table but leaves the table structure intact.
DELETE selectively removes rows based on specified conditions and generates
individual transaction log entries for each deleted row.
Question: Explain the different types of joins in SQL and provide an example of when you
would use each type.
Answer: Joins like INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN are used to
combine rows from different tables. For example, an INNER JOIN is used when you want to
retrieve rows that have matching values in both tables.
Question: What is a subquery, and how would you use it to retrieve specific information from a
database?
Answer: A subquery is a query nested inside another query. It can be used to retrieve data that
will be used by the main query for further processing.
Question: Explain the concept of window functions in SQL and provide an example of when
you would use them.
Answer: Window functions perform calculations across a specified range of rows related to the
current row. They are used for tasks like ranking, aggregation, and cumulative totals.
Question: Describe the steps you would take to migrate data from one database to another,
ensuring data integrity.
Answer: Data migration involves exporting data from the source, transforming it as needed, and
importing it into the destination while ensuring data consistency and accuracy.
Question: What are stored procedures and triggers, and in what situations would you use
them?
Answer: Stored procedures are precompiled SQL code that can be reused, and triggers are
special types of stored procedures that are automatically executed in response to certain
events.
Question: Explain how NULL values are handled in SQL, and how you would retrieve or handle
them in query results.
Answer: NULL is used to represent missing or undefined data. Functions like COALESCE or IS
NULL can be used to handle NULL values in queries.
Question: Discuss the importance of transactions in a database and how you would ensure
data consistency.
Answer: Transactions are sequences of one or more SQL statements that are executed as a
single unit. They ensure data consistency by either committing all changes or rolling back if an
error occurs.
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 123;
COMMIT;
Question: Explain the concept of data encryption in a database and why it is important for
security.
Answer: Data encryption involves converting data into a code to prevent unauthorized access.
It is crucial for protecting sensitive information stored in databases.
Question: What is dynamic SQL, and under what circumstances would you use it?
Answer: Dynamic SQL involves constructing and executing SQL statements at runtime. It is
used when the structure of a query is not known until the application is running.