The document provides an overview of SQL, including its definition, differences from NoSQL, and various SQL concepts such as joins, primary and foreign keys, stored procedures, triggers, and ACID properties. It also covers SQL commands, error handling, normalization, and aggregate functions, along with examples of SQL queries for data manipulation and retrieval. Additionally, it explains optimization techniques and constraints in SQL databases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
1 views4 pages
SQL
The document provides an overview of SQL, including its definition, differences from NoSQL, and various SQL concepts such as joins, primary and foreign keys, stored procedures, triggers, and ACID properties. It also covers SQL commands, error handling, normalization, and aggregate functions, along with examples of SQL queries for data manipulation and retrieval. Additionally, it explains optimization techniques and constraints in SQL databases.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
1.What is SQL?
Structured query language (SQL) is a programming language for storing and
processing information in a relational database. A relational database stores information in tabular form, with rows and columns representing different data attributes and the various relationships between the data values. 2.Explain the differences between SQL and NoSQL databases. While SQL databases are best used for structured data, NoSQL databases are suitable for structured, semi-structured, and unstructured data. 3.Explain different types of joins with examples. A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Different Types of SQL JOINs: (INNER) JOIN: Returns records that have matching values in both tables LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table. 4.What is a primary key in SQL? The primary key in SQL is a single, or a group of fields or columns that can uniquely identify a row in a table. Putting it simply, it is a column that accepts unique values for each row. 5.What is a foreign key? A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table. A foreign key links two or more tables in a relational database. 6. How do you optimize SQL queries? *Use indexes effectively. *Avoid SELECT * and retrieve only necessary columns. *Optimize JOIN operations. *Minimize the use of subqueries. *Avoid redundant or unnecessary data retrieval. *Utilize stored procedures. *Consider partitioning and sharding. *Normalize database tables. 7. How do you create a stored procedure? A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it. Expand the database that you want, and then expand Programmability. Right-click Stored Procedures, and then select New > Stored Procedure. A new query window opens with a template for the stored procedure. 8. What is a trigger in SQL? A trigger is a database object that is automatically executed or fired when certain events (INSERT, UPDATE, DELETE) occur on a table. 9. Explain ACID properties in SQL: ACID stands for: • Atomicity: All operations in a transaction are completed; if not, the transaction is aborted. • Consistency: The database remains in a consistent state before and after the transaction. • Isolation: Each transaction is isolated from others. • Durability: Once a transaction is committed, its changes are permanent. 10. What is a transaction in SQL? A transaction is a group of one or more SQL operations that are executed as a single unit of work. It ensures that database changes are reliable and consistent. 11. Explain normalization and denormalization. • Normalization: The process of organizing data in a database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller ones and linking them using foreign keys. • Denormalization: The process of combining tables to reduce the number of joins in queries and improve performance, at the cost of redundancy. 12. What is a view in SQL? A view is a virtual table that represents the result of a query. It does not store data but provides a way to simplify complex queries. 13. How do you prevent SQL injection? • Use Prepared Statements: Ensure user inputs are parameterized. • Use Stored Procedures: Avoid dynamic SQL where possible. • Input Validation: Sanitize inputs by checking type, length, format, etc. • Escape Special Characters: Use proper escaping when necessary. 14. What are indexes in SQL? An index is a database object that improves the speed of data retrieval operations on a table at the cost of additional space and write-time overhead. 15. Difference between UNION and UNION ALL: • UNION: Combines results of two queries, removing duplicate rows. • UNION ALL: Combines results of two queries, including duplicate rows. 16. Types of SQL Commands: SQL commands are grouped into categories: • DDL (Data Definition Language): CREATE, ALTER, DROP. • DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE. • DCL (Data Control Language): GRANT, REVOKE. • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT. 17. What is a JOIN in SQL, and what are its types? A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Different Types of SQL JOINs: (INNER) JOIN: Returns records that have matching values in both tables LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table. 18. What is a constraint in SQL? Name a few. A constraint is a rule applied to columns in a table to enforce data integrity. • Primary Key: Uniquely identifies each row. • Foreign Key: Ensures referential integrity. • Unique: Ensures no duplicate values. • Not Null: Ensures a column cannot have NULL values. • Check: Ensures values in a column satisfy a specific condition. 19. What is a trigger in SQL? A trigger is a database object that is automatically executed or fired when certain events (INSERT, UPDATE, DELETE) occur on a table. 20. What are aggregate functions? Can you name a few? Aggregate functions operate on a set of rows and return a single value. • COUNT: Counts the number of rows. • SUM: Calculates the sum of values. • AVG: Calculates the average value. • MIN: Returns the minimum value. • MAX: Returns the maximum value. 21. Explain different types of joins with examples. A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Different Types of SQL JOINs: (INNER) JOIN: Returns records that have matching values in both tables LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table. 22. How do you implement error handling in SQL? Error handling in SQL allows you to manage and respond to errors that occur during the execution of your queries or scripts. A group of Transact-SQL statements can be enclosed in a TRY block. If an error occurs in the TRY block, control is usually passed to another group of statements that is enclosed in a CATCH block. 23. Explain all DDL & DML commands? SQL (Structured Query Language) is used to interact with databases. The commands in SQL are categorized into different types based on the operations they perform. Two important categories are DDL (Data Definition Language) and DML (Data Manipulation Language). DDL (Data Definition Language): DDL commands are used to define, modify, or delete database structures such as tables, indexes, and schemas. These commands affect the structure of the database but do not directly manipulate the data inside the tables. CREATE: • Used to create new database objects like tables, views, indexes, or databases. ALTER: • Used to modify an existing database object, such as adding, deleting, or modifying columns in a table. DROP: • Used to delete an existing database object, like a table, view, or index. TRUNCATE: • Used to delete all rows from a table, but the table structure (columns, constraints) remains. RENAME: • Used to rename a database object like a table or column. DML (Data Manipulation Language): DML commands are used to manipulate the data inside the database tables. These commands allow you to insert, update, delete, or retrieve data from tables. Common DML Commands: SELECT: • Used to retrieve data from one or more tables. INSERT: • Used to add new rows (data) into a table. UPDATE: • Used to modify existing data in a table. DELETE: • Used to remove data (rows) from a table. 24. SQL Query for Retrieving Tables. To retrieve a list of all tables in a database, you can use the SHOW TABLES command in MySQL, or a query against the information_schema in other databases. 25. Query for Selecting Columns from a Table To select specific columns from a table, you use the SELECT statement and specify the columns. Syntax: SELECT column1, column2, column3 FROM table_name; 26. Query for Outputting Data Using a Constraint. To retrieve data based on a condition (a constraint), you can use the WHERE clause. Syntax: SELECT * FROM table_name WHERE column_name = 'value'; 27. Query for Outputting Sorted Data Using ORDER BY. To sort the results, use ORDER BY. You can sort in ascending (ASC) or descending (DESC) order. Syntax: SELECT * FROM table_name ORDER BY column_name ASC; -- For ascending order. 28. SQL Query for Outputting Sorted Data Using GROUP BY GROUP BY is used to group rows that have the same values in specified columns into aggregated data, often used with functions like COUNT(), SUM(), etc. Syntax: SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name; 29. Data Manipulation Using COUNT COUNT() is an aggregate function that returns the number of rows in a group. Syntax: SELECT COUNT(*) FROM table_name; 30. SQL Query for Listing All Views To list all views in the database, you can query the information_schema.views table. • PostgreSQL, SQL Server, MySQL: Syntax: SELECT table_name FROM information_schema.views WHERE table_schema = 'public'; 31. Query to Display Primary Keys. To retrieve information about primary keys in a table, you can use the information_schema database. 32. Query for Displaying Foreign Keys. To display foreign keys in a table, you can use the information_schema to look for constraints of type FOREIGN KEY. 33. Swapping the Values of Two Columns in a Table. To swap values of two columns for certain rows, you can use the UPDATE statement. Syntax: UPDATE table_name SET column1 = column2, column2 = column1; 34. SQL Query for Dropping a View To drop (delete) an existing view, you can use the DROP VIEW command. Syntax: DROP VIEW view_name; 35. Displaying Triggers To display triggers in the database, you can query the information_schema.triggers table