Import Theory Question - SQL
Import Theory Question - SQL
SQL
1. Degree – It refers to the number of columns in a relation
Cardinality – It refers to the number of rows in a relation
4. Constraints:
Constraints are certain types of restrictions on the data values that an attribute
can have.
Constraint Description
NOT NULL Ensures that a column cannot have NULL values where NULL
means missing/ unknown/not applicable value.
UNIQUE Ensures that all the values in a column are distinct/unique.
DEFAULT A default value specified for the column if no value is provided.
PRIMARY KEY The column which can uniquely identify each row or record in a
table.
FOREIGN KEY The column which refers to value of an attribute defined as primary
key in another table.
5. Difference between Char and Varchar Data type:
char varchar
Used for fixed-length character Used for variable-length character
strings. strings.
It can store maximum of 255 It can store maximum of 65,535
characters characters
It pads unused space with blanks It holds only the characters
assigned to it.
Memory allocation is static Memory allocation is dynamic
Faster than VARCHAR due to fixed Slower than CHAR due to variable
size. length
6. SQL Commands:
(i) DDL(Data Definition Language):
> create
> alter
> drop
> truncate
(ii) DML(Data Manipulatrion Language):
> insert
> update
> delete
(iii) DQL(Data Query Language):
> Select
7. Difference Primary Key and Unique Key
9. where clause: The where clause is used to filter the records of a table based on the
specified condition
10 . group by:
GROUP BY statement is used to group rows based on values in one or more columns.
It's particularly useful when you want to calculate aggregate values (such as count, average,
sum, or maximum) for each group.
11. Having Clause:
• The HAVING clause in SQL is used to filter query results based on aggregate functions
and groupings.
• The HAVING clause allows you to filter groups of data after using the GROUP BY clause.
12. Order by:
• The ORDER BY clause is used to sort the result set in either ascending or descending
order based on one or more columns.
• By default, the ORDER BY command sorts the result set in ascending order.
• If you want to sort the records in descending order, you can use the DESCkeyword.
SELECT * FROM Customers
ORDER BY CustomerName DESC;
13. Difference where and having
WHERE Clause HAVING Clause
Used to filter rows of a table. User to filter the grouped data.
It can be used without GROUP BY clause. To be used with GROUP BY clause.
Operates on individual records. Operates on grouped records.
Applied before GROUP BY clause. Applied after GROUP BY clause.
Can contain aggregate functions (e.g., SUM,
Cannot contain aggregate functions. COUNT).
Used with SELECT, UPDATE, and DELETE statements. Used only in SELECT statements.
Eg: To remove a record with empid = 1001 from Eg:To remove/delete the employee table:
employee table DROP TABLE employee;
delete from employee where empid = 1001;
Actions can be rolled back. Actions cannot be rolled back.
15. Difference Group by and order by:
GROUP BY ORDER BY
It is used to groups rows based on the same value in It is used sorts the result set in ascending or descending
specified columns. order based on one or more columns.
Often used with aggregate functions (e.g., COUNT,
SUM, AVG) to compute statistics for groups. Used to arrange the output rows.
Requires an aggregate function (e.g., COUNT, SUM)
when used. Does not require an aggregate function.
Columns in the SELECT clause must be either part of
the grouping columns or aggregate functions. Can select any columns in the SELECT clause.
SELECT column1, column2 FROM table ORDER BY
SELECT column1, aggregate_function(column2) column1 ASC; or SELECT column1, column2 FROM
FROM table GROUP BY column1; table ORDER BY column1 DESC;
SQL Commands
(i) Creating database:
Syntax:
Create database databaseName;
Eg:
Create database Class11b23;
Syntax:
Use databaseName;
Eg:
Use Class11b23;
(iii) To view the available databases:
Show databases;
(iv) To create a table:
Syntax:
Create table tableName(col1Name datatype [constraint], Col2Name datatype [constraint],
….., ColN_Name datatype [constraint]);
Student