0% found this document useful (0 votes)
8 views

SQL

Uploaded by

Navya Shree
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)
8 views

SQL

Uploaded by

Navya Shree
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
You are on page 1/ 4

1.

Basic SQL Queries:


• To select all records from a table, you can use SELECT * FROM table_name;.
• To select specific columns from a table, use SELECT column1, column2 FROM table_name;.
• To filter rows using the WHERE clause, add WHERE followed by your condition, like SELECT
* FROM table_name WHERE column_name = 'value';.
• To sort results using ORDER BY, use SELECT * FROM table_name ORDER BY column_name;.
• To limit the number of results using LIMIT, add LIMIT followed by the number, e.g.,
SELECT * FROM table_name LIMIT 10;.

2. Joins:
• Different types of SQL joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
These combine rows from two or more tables based on related columns.
• To join tables, use a query like SELECT * FROM table1 INNER JOIN table2 ON table1.column =
table2.column;.

3. Aggregation Functions:
• SQL aggregation functions (e.g., SUM, COUNT, AVG, MAX, MIN) perform calculations on
columns in a result set.
• Example: SELECT AVG(column_name) FROM table_name; calculates the average of a column.

4. Grouping and HAVING:


• GROUP BY is used to group rows based on a specified column.
• HAVING filters grouped results.
• Example: SELECT department, AVG(salary) FROM employees GROUP BY department HAVING
AVG(salary) > 50000;.

5. Subqueries:
• A subquery is a query nested within another query. It can be used to retrieve data for
use in the main query.
• Example: SELECT * FROM table1 WHERE column1 IN (SELECT column2 FROM table2);.

6. Constraints:
• Primary keys uniquely identify rows in a table.
• Foreign keys establish relationships between tables.
• Example: CREATE TABLE orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY
(customer_id) REFERENCES customers(customer_id));.

7. Normalization:
• Database normalization is the process of organizing data to minimize redundancy and
dependency.
• Benefits include reduced data duplication and improved data integrity.
8. Indexes:
• An index is a data structure that improves the speed of data retrieval operations on a
database table.
• Types include B-tree and Hash indexes.

9. Transactions:
• A database transaction is a sequence of SQL statements treated as a single unit.
• ACID properties ensure reliability: Atomicity, Consistency, Isolation, Durability.

10. Views:
• A database view is a virtual table based on the result of a SELECT query.
• Views simplify complex queries and restrict access to data.

11. Stored Procedures and Functions:


• Stored procedures are sets of SQL statements executed as a single unit.
• Functions return a value.
• Example:
sqlCopy code
CREATE PROCEDURE sp_example AS BEGIN -- SQL statements END ;

12. Normalization Beyond 3rd Normal Form:


• 4th and 5th Normal Forms deal with multi-valued and join dependencies.
• They are essential for very complex data models.

13. SQL Injection:


• SQL injection is a security vulnerability where malicious SQL queries are injected into an
application's input fields.
• Prevent it by using prepared statements or parameterized queries.

14. Indexes and Query Optimization:


• Optimize slow queries by using indexes, writing efficient queries, and denormalizing
data when appropriate.

15. Backup and Restore:


• Use database-specific commands like mysqldump for MySQL to backup and restore
databases.

16. Triggers:
• Triggers are automatic actions that occur in response to specific database events.
• They can be used for data validation, auditing, or maintaining data integrity.
17. NoSQL vs. SQL:
• Compare SQL databases (structured, relational) with NoSQL databases (unstructured or
semi-structured).
• Choice depends on data structure and scalability needs.

18. Self-Joins:
• A self-join is when a table is joined with itself. It's useful for hierarchical data structures.
• Example: SELECT e1.name, e2.name FROM employees e1 INNER JOIN employees e2 ON
e1.manager_id = e2.employee_id.

19. UNION vs. UNION ALL:


• UNION combines and deduplicates rows, while UNION ALL combines all rows, including
duplicates.

20. Differences Between SQL Databases:


• Compare SQL databases like MySQL, PostgreSQL, and SQL Server in terms of features,
performance, and licensing.

21. Database Design:


• Database design involves defining tables, relationships, constraints, and data types to
create an efficient schema.

22. Data Migration:


• Data migration involves transferring data from one database to another using tools or
scripts.

23. Data Types:


• SQL data types include INT, VARCHAR, DATE, among others, each suited for different
types of data.

24. SQL Performance Tuning:


• Techniques for optimization include indexing, query rewriting, and hardware upgrades.

25. Data Modeling:


• Data modeling is the process of creating a visual representation of data structures and
relationships.

26. Concurrency Control:


• SQL handles concurrent transactions using locking mechanisms and isolation levels to
maintain data consistency.

27. Triggers vs. Stored Procedures:


• Triggers are event-driven, while stored procedures are user-invoked.
28. Database Security:
• Secure a SQL database by using strong authentication, access control, and encryption.

29. SQL vs. NoSQL Use Cases:


• Choose SQL for structured data with complex relationships and NoSQL for flexible,
unstructured data.

30. Database Sharding:


• Database sharding involves partitioning data across multiple servers for scalability.

31. Temporal Tables:


• Temporal tables track changes to data over time, useful for historical data analysis.

32. Database Scaling Strategies:


• Horizontal scaling adds more servers, while vertical scaling increases server resources.

33. Database Backup and Recovery Strategies:


• Strategies include full, differential, and incremental backups, with regular testing of
recovery procedures

You might also like