Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

SQL_AI_KIT

The document provides an overview of SQL concepts, including types of joins, triggers, indexes, normalization, and SQL commands. It explains the differences between primary keys and foreign keys, as well as the operations performed in DBMS and RDBMS. Additionally, it covers data security, querying, and the prevention of SQL injection, along with the use of aggregation functions.

Uploaded by

kushalthadi143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL_AI_KIT

The document provides an overview of SQL concepts, including types of joins, triggers, indexes, normalization, and SQL commands. It explains the differences between primary keys and foreign keys, as well as the operations performed in DBMS and RDBMS. Additionally, it covers data security, querying, and the prevention of SQL injection, along with the use of aggregation functions.

Uploaded by

kushalthadi143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL_AI_KIT

22 September 2023 17:16

Define joins and list the types of joins?

Joins are operations in databases that combine data from multiple tables
based on related columns. The types of joins include inner join, natural
join, left join, right join, full join, cross join, and self join.

Explain about LEFT JOIN and RIGHT JOIN?

Left join is used to combine all the rows from the left table with the
matching rows from the right table. If there is no match, NULL values are
assigned to the right half of the rows in the temporary table. Right join is
used to combine all the rows from the right table with the matching rows
from the left table. If there is no match, NULL values are assigned to the
left half of the rows in the temporary table.

Explain about INNER JOIN and FULL JOIN?

Inner join is the most common type of join that is used to return only the
rows that have matching values in both tables. A full join is used to
combine all rows from both the right and left tables. It includes matching
rows from both tables and if there is no match, NULL values are
assigned to the non-matching columns from either table.

What is CROSS-JOIN?

A cross-join or Cartesian join combines every row from the first table
with every row from the second table, resulting in a Cartesian product.

What is SELF-JOIN?

A self-join is when a table is joined with itself, it combines the records of a


table with itself. It's like looking at the same table from two different angles.
It helps us compare or analyze data within the table itself.

How to get the names from database?

To get the names from a database, you can utilize SQL queries. Assuming
there is a table named 'users' with a column named 'name', you can
execute the following SQL query to obtain all the names from the database:
```sql SELECT name FROM users; ```

What are triggers in SQL?

Triggers in SQL are special database objects that are automatically


New Section 4 Page 1
Triggers in SQL are special database objects that are automatically
executed in response to specific events, such as data modifications
(inserts, updates, deletes). They allow you to define custom actions to be
performed, such as updating related tables or enforcing data integrity
constraints.

Explain the types of triggers in SQL

DML Triggers: These triggers are executed in response to Data


Manipulation Language (DML) statements such as INSERT, UPDATE, or
DELETE. They are used to enforce data integrity, perform auditing, or
automate data-related actions. DDL Triggers: These triggers are executed
in response to Data Definition Language (DDL) statements such as
CREATE, ALTER, or DROP. They are used to track schema changes,
enforce security policies, or perform specific actions when database objects
are modified.

What is Index in SQL?

In databases, we maintain indexes to speed up the search for data in a


table. It improves the performance of queries. It works like an index in a
book, allowing for faster data retrieval.

Explain the different types of indexes in SQL

The types of indexes in SQL include clustered indexes, which determine


the physical order of data in a table, and non-clustered indexes, which
create a separate structure to improve search performance.

How do you retrieve data from database?

To retrieve data from a database, you can use the SQL SELECT clause.
Syntax: SELECT column1, column2, column3 FROM table_name

What is SQL?

SQL stands for Structured Query Language and is used to perform various
operations on Relational DBMS. These operations includes creating tables,
querying data, inserting, updating, and deleting records.

What will be the result when a join operation is performed on two tables?

When you perform a join operation on two tables, the result is a new table

New Section 4 Page 2


When you perform a join operation on two tables, the result is a new table
that combines rows from both tables based on a specified condition. This
resulting table includes columns from both tables and only includes rows
that satisfy the join condition. For example, let's take "TableA" and "TableB"
as the two tables. If you perform a join using a common column like "id",
the output will contain rows where the "id" values match in both tables.

What are the different normal forms in database normalization?

Database normalization has different normal forms to guide the process of


organizing data. The commonly known normal forms are 1NF (First Normal
Form), 2NF (Second Normal Form), 3NF (Third Normal Form), and BCNF
(Boyce-Codd Normal Form). Each normal form has specific rules and
requirements for reducing data redundancy and ensuring data integrity.

Define First Normal Form (1NF) in database normalization?

First Normal Form (1NF) in database normalization requires that a table


has a primary key and that each column contains only atomic values
(indivisible and non-repeating). It eliminates repeating groups by ensuring
that each attribute holds a single value, promoting data integrity and
enabling efficient querying.

What is a stored procedure in SQL?

A stored procedure in SQL is essentially a set of SQL commands that are


saved as single logic and reused. It is defined using the "CREATE
PROCEDURE" statement, it groups a series of SQL commands as a single
logic,After creation, this procedure can be called using the `EXECUTE` or
`CALL` command.

Define Second Normal Form (2NF) in database normalization?

Second Normal Form (2NF) is a database normalization technique that


ensures the removal of partial dependencies in a relation. It requires the
data to meet the criteria of being in First Normal Form (1NF) and further
mandates that non-key attributes are functionally dependent on the entire
primary key.

Define Third Normal Form (3NF) in database normalization?

The Third Normal Form (3NF) in database normalization extends 2NF and
states that a table must meet 2NF requirements, and there should be no
transitive dependencies. This means that non-key attributes should not
depend on other non-key attributes.

New Section 4 Page 3


depend on other non-key attributes.

What is the difference between a primary key and a foreign key in SQL?

A primary key uniquely identifies each row in a table, while a foreign key
establishes relationships between tables by referencing the primary key of
another table. Primary keys ensure uniqueness, while foreign keys
maintain referential integrity in SQL databases.

What is the difference between DBMS and RDBMS ?

The main difference between DBMS and RDBMS lies in their approach to
managing data. RDBMS stores the data in the form of tables while DBMS
stores data without organizing it into tables. DBMS focuses on data storage
and retrieval, while RDBMS adds the capability to use relationships and
constraints.

What are the differences between the WHERE and HAVING clauses?

The WHERE and HAVING clauses are used in SQL queries to filter data,
but they are applied at different stages of the query execution. The WHERE
clause filters rows based on column values before any grouping or
aggregation is done. It operates on individual rows. On the other hand, the
HAVING clause filters grouped rows after aggregation. It works on
aggregated values within groups.

What is the difference between DROP, DELETE, and TRUNCATE


statements in SQL?

The DROP clause is used to permanently remove an entire database


object, like a table or a database itself. The DELETE clause is used to
remove specific rows from a table. You can use conditions to specify which
rows you want to delete. The TRUNCATE clause. It's a bit different from
DELETE. When you use TRUNCATE, it removes all rows from a table,
basically giving it a fresh start..

Explain the concept of normalization in databases.

Normalization is the process of organizing and structuring a database to


eliminate redundancy and improve data integrity. It involves breaking down
a large table into smaller, more manageable tables and defining
relationships between them. Normalization follows a set of rules, called
normal forms, to ensure data is stored efficiently and without data
anomalies. This helps in reducing data duplication, improving data
consistency, and enhancing database performance.

New Section 4 Page 4


What is the purpose of a VIEW in SQL?

A view can simply be considered as a name to a SQL Query. A "VIEW" in


SQL is a virtual table that is derived from the result of a previous SQL
query. The purpose of a view is to provide a simplified and customized
representation of the data stored in one or more tables.

What are functions in SQL?

In SQL, functions are predefined operations that perform specific tasks on


the data in a database. They can take input parameters, perform
calculations, manipulate strings or dates, aggregate data, and return a
result. Functions increase the capabilities of SQL by providing reusable and
efficient operations for data manipulation and analysis.

How can we use functions in SQL?

Functions in SQL, either built-in or user-defined, perform specific tasks


during data retrieval, manipulation, or transformation. They're used within
SQL statements, including SELECT, WHERE, GROUP BY, HAVING, and
JOIN clauses, making queries more modular, efficient, and expressive.

What is the purpose of the ORDER BY command in SQL?

In SQL, the ORDER BY command is used to sort the results of a query


based on one or more columns. This can be in ascending (ASC) or
descending (DESC) order. It's usually used with the SELECT statement.

What is the purpose of the HAVING command in SQL?

The HAVING command in SQL is used to filter the result set of a query
based on conditions applied to aggregated data. It is similar to the WHERE
clause but operates on the grouped data generated by the GROUP BY
clause. The HAVING command allows you to specify conditions that
involve aggregate functions like SUM, COUNT, AVG, etc.

What is the purpose of the GROUP BY command in SQL?

The GROUP BY command in SQL is used to group rows from a table


based on one or more columns. It is commonly used with aggregate
functions to perform calculations on groups of data. The GROUP BY clause
divides the result set into groups based on the specified columns, and the
aggregate functions are applied to each group separately.
New Section 4 Page 5
aggregate functions are applied to each group separately.

What are the operations performed in DBMS?

In a Database Management System (DBMS), you can perform different


operations to manage and manipulate data. These operations, commonly
known as CRUD operations: 1. Create 2. Retrieve 3. Update 4. Delete

What is a foreign key?

A foreign key is a column in a database table that creates a link to the


primary key column of another table. This linkage helps maintain data
consistency and supports relationships between tables in a relational
database.

How many types of clauses do we have in SQL?

SQL provides multiple clauses for managing and querying relational


databases. These include SELECT, FROM, WHERE, GROUP BY,
HAVING, ORDER BY, JOIN, LIMIT, DISTINCT, among others.

What types of files are stored in DBMS and RDBMS?

In a DBMS and RDBMS, various types of files are stored, including data
files, index files, and log files. Data files hold the actual data records, index
files help optimize data retrieval, and log files record transactions and
ensure data consistency.

What is the difference between DDL and DML?

DDL, or Data Definition Language, manages a database structure by


creating, altering, or deleting tables. On the other hand, DML, or Data
Manipulation Language, manipulates stored data through actions such as
inserting, updating, or deleting records.

How can you alter or stop a trigger?

To modify an existing trigger, you must drop the existing trigger using the
"DROP TRIGGER" command and then recreate it with the desired
modifications. SQLite does not support the "ALTER TRIGGER"

What is the purpose of Data Definition operations in a DBMS?

New Section 4 Page 6


What is the purpose of Data Definition operations in a DBMS?

Data Definition operations in a DBMS define the structure of the database


and its objects. This includes creating tables, specifying attributes and data
types, setting up table relationships, and defining constraints. These
operations establish the foundation of the database and determine how
data is organized and stored.

What are Data Manipulation operations in DBMS?

Data Manipulation operations in a DBMS involve manipulating the data


stored in the database. These operations include retrieving data using
SELECT statements, inserting new data using INSERT statements,
updating existing data using UPDATE statements, and deleting data using
DELETE statements. Data Manipulation operations allow users to interact
with the database and modify its contents.

What is the purpose of Data Control operations in a DBMS?

Data Control operations in a DBMS ensure data integrity, security, and


access control. They define user permissions and roles, set up constraints
and validations to maintain data integrity, implement data encryption for
protection, and manage backups and recovery processes. These
operations maintain the security and reliability of the database.

What are Data Querying and Reporting operations in DBMS?

Data Querying and Reporting operations in a DBMS involve retrieving


specific information and generating reports. SQL is used to perform tasks
like filtering, sorting, joining, and aggregating data. These operations help
extract insights from the database and present data in an organized format
for analysis and decision-making.

What is a database?

A database is a structured collection of data stored in a computer system. It


allows efficient storage, retrieval, and management of large amounts of
information.

What are the features of a database related to data organization and


integrity?

Data Organization: Databases organize data into tables, rows, and


columns, providing a structured format for managing information. Data
Integrity: Databases enforce data integrity through constraints like primary
keys, unique keys, and foreign keys. These constraints ensure accurate,
consistent, and reliable data by preventing inconsistencies and maintaining
New Section 4 Page 7
consistent, and reliable data by preventing inconsistencies and maintaining
relationships between tables.

What are the features of a database related to data security and querying?

Database Security: Databases offer security features to protect data from


unauthorized access and maintain privacy. These include user
authentication, access control, encryption, and auditing to safeguard
sensitive information. Data Querying: Databases provide query languages
like SQL for retrieving, manipulating, and analyzing data. Query languages
offer operations such as filtering, sorting, joining, and aggregating data,
enabling users to extract specific information and gain insights from the
database.

What are the features of a database related to transactions, scalability, and


data recovery?

Databases support transactions, grouping multiple operations into a single


unit. This ensures data consistency by applying all changes successfully or
none at all. They also handle large data volumes and optimize storage,
indexing, and query processing for scalability. Databases provide
mechanisms for data backup and recovery, allowing data restoration in
case of failures or errors.

What is a non-relational database?

A non-relational database, or NoSQL, provides a flexible and scalable way


to store data without fixed tables. It supports various data models like key-
value, document, columnar, and graph databases. Non-relational
databases handle unstructured data, scale horizontally, and offer high
availability and performance. Popular examples include MongoDB,
Cassandra, Redis, and Couchbase.

What are the advantages of using DBMS?

Advantages of using a DBMS includes: 1. Security: Data is stored &


maintained securely. 2. Ease of Use: Provides simpler ways to create &
update data at the rate it is generated and updated respectively. 3.
Durability and Availability: Durable and provides access to all the clients at
any point in time. 4. Performance: Quickly accessible to all the
clients(applications and stakeholders).

Define SQL injection, and explain the methods to prevent it?

SQL injection is a security issue where attackers manipulate input data to


execute harmful SQL statements. It can result in unauthorized access, data

New Section 4 Page 8


execute harmful SQL statements. It can result in unauthorized access, data
breaches, or database manipulation. To prevent SQL injection, follow these
methods: 1. Use prepared statements or parameterized queries. 2. Validate
user input to ensure it meets expected criteria. 3. Manage user privileges
appropriately. Avoid directly concatenating user input into SQL statements.
Instead, sanitize input by escaping special characters or utilize database
APIs that handle input securely.

What are aggregations in SQL?

Aggregations in SQL are functions that perform calculations on a set of


values and return a single result. Common aggregations include SUM
(calculates the sum of values), COUNT (counts the number of rows), AVG
(calculates the average of values), MIN (finds the minimum value), and
MAX (finds the maximum value). Aggregations are often used with the
GROUP BY clause to perform calculations on grouped data.

What is the use of the ORDER BY clause in SQL?

The ORDER BY clause in SQL is used to sort the result set of a query. It
specifies the columns by which the data should be sorted, such as
ascending (ASC) or descending (DESC) order. The query returns the rows
in the specified order based on the values in the specified column(s). For
example, "ORDER BY column_name ASC" would sort the data in
ascending order based on the values in the specified column.

What is the use of the GROUP BY clause in SQL?

The GROUP BY clause in SQL is used to group rows based on a specified


column. It allows aggregation functions like SUM, COUNT, AVG, etc., to be
applied to each group. The result is a summary of data where rows with the
same values in the specified column are combined into groups, and
calculations are performed on each group rather than individual rows.

What are constraints in SQL?

Constraints in SQL are rules applied to table columns to ensure accurate


and reliable data storage. They limit the type of data allowed, maintain data
integrity, and establish table relationships.

What are the various types of constraints used in SQL?

SQL uses various types of constraints, including: PRIMARY KEY: Uniquely


identifies each record. FOREIGN KEY: Links tables together for referential
integrity. UNIQUE: Ensures values in a column are unique. CHECK:

New Section 4 Page 9


integrity. UNIQUE: Ensures values in a column are unique. CHECK:
Defines conditions for data insertion or update. NOT NULL: Prevents NULL
values in a column. DEFAULT: Sets a default value for a column if not
specified. These constraints can be applied at the column or table level.

What are keys in SQL?

Keys in SQL are used to uniquely identify rows in a table, establish


relationships between tables, and ensure data integrity. There are different
types of keys in SQL: Primary Key, Foreign Key, unique Key etc. Keys play
a crucial role in ensuring the uniqueness of rows in a table and establishing
relationships between tables while preserving data integrity.
Define primary key, foreign key, and unique key

A primary key is a column or a set of columns that uniquely identifies each


row in a table. A table can have only one primary key, and its values must
be unique and not null. A foreign key is a column or a set of columns that
refer to the primary key of another table. It is used to establish a
relationship between two tables and enforce referential integrity. A unique
key is a column or a set of columns that enforce the uniqueness of values
in a table. Unlike primary keys, a table can have multiple unique keys, and
their values can be null.

What is store procedure?

A stored procedure is like a named code block in a database that can be


used over and over. It's handy for doing complex tasks, working with data,
and giving back results. Stored procedures make code reusable, help
databases run faster, and make things more secure.

Explain the difference between SQL and NoSQL databases.

SQL databases use structured tables with fixed schemas, while NoSQL
databases use flexible structures like key-value pairs or documents. SQL
databases handle complex data relationships through joins, combining
information from multiple tables, while NoSQL databases focus on efficient
retrieval within a single document or key-value pair.

What is the MOD function in SQL?

In SQL, the MOD function (short for modulo) calculates the remainder
when one number is divided by another. It returns the remainder value.
Example: SELECT MOD(17, 5); The query above returns the remainder 2
when 17 is divided by 5.

New Section 4 Page 10


How to use wildcards?

Wildcards are used in SQL with the LIKE operator to search for specific
patterns in strings. The two common wildcards are '%' and '_'. The '%'
wildcard represents any sequence of characters, while the '_' wildcard
represents a single character. For example, to find all names starting with
'A', you can use: SELECT * FROM table_name WHERE name LIKE 'A%';

How to design database schema and implement in PostgreSQL?

To design a database schema and implement it in PostgreSQL, you can


use SQL statements. Start by identifying the entities and their relationships.
Then create tables using the CREATE TABLE statement, define columns
with data types, set primary keys, and establish foreign key relationships.
Finally, insert data using INSERT INTO statements.

What are the types of commands in SQL?

SQL commands are categorized into DDL for managing database


structures with commands such as CREATE, DROP and ALTER, DML for
handling database data with commands such as INSERT, DELETE and
UPDATE, DQL for retrieving data from database using SELECT, DCL for
controlling access to data in database with commands like GRANT and
REVOKE, and TCL for overseeing transactions with commands such as
COMMIT and ROLLBACK.

What is the difference between 'commit' and 'rollback'?

In database transactions, `commit` and `rollback` maintain data integrity.


`Commit` permanently saves transaction changes, making them
irreversible and visible to others. Conversely, `rollback` undoes all changes
in a transaction, retaining the database's previous state before the
transaction. It is used to correct errors, ensuring no flawed data is saved.
Both commands facilitate reliable and accurate database management by
confirming or reversing transaction changes accordingly.

Where do we use clauses such as 'where' and 'having'?

In SQL, the "WHERE" clause is used to filter rows based on individual


column values before groupings or aggregations occur. On the other hand,
the "HAVING" clause is employed to filter grouped or aggregated data,
functioning alongside the "GROUP BY" clause in SELECT statements.
"WHERE" filters at a row level, while "HAVING" filters at a group level.

New Section 4 Page 11


"WHERE" filters at a row level, while "HAVING" filters at a group level.

Explain the different types of joins available in SQL.

In SQL, joins merge rows from different tables based on a related column.
There are different types of joins. The "Inner Join" returns records that
match in both tables. The "Left Join" returns all from the left, matched from
the right. The "Right Join" is the opposite of "Left Join". The "Full Join" rows
from both tables with matching and non-matching values. The "Cross Join"
returns all possible row combinations. The "Self Join" returns a table joins
with itself and "Natural Join" joins using columns with the same name and
type.

List five commands used in DML.

"INSERT" commmad to add new records to a database table. "UPDATE"


command to modify existing records in a database. "DELETE" command
To remove records from a database. "MERGE" command to combine insert
and update operations based on certain conditions. "CALL" command to
invoke stored PL/SQL or Java subprograms.

What is the difference between the left operand and the right operand?

In SQL, the "left operand" is the initial value in an operation, situated to the
left of the operator. Conversely, the "right operand" is the comparative
value, located to the right of the operator.

New Section 4 Page 12

You might also like