You are on page 1of 7

Important Theory Questions – Must Read

SQL
 FUNCTIONS IN SQL

Date Functions
Function Description Example with output
NOW() It returns the current system date and time. mysql> SELECT NOW();
Output:2024-03-11 09:41:17
DATE() It returns the date part from the given date/ time mysql> SELECT DATE(NOW());
expression. Output: 2024-03-11
MONTH(date) It returns the month in numeric form from the date. mysql> SELECT MONTH(NOW());
Output: 3
MONTHNAME(date) It returns the month name from the specified date. mysql> SELECT MONTHNAME(“2003-11-28”);
Output:November
YEAR(date) It returns the year from the date. mysql> SELECT YEAR(“2003-10-03”);
Output:2003
DAY(date) It returns the day part from the date. mysql> SELECT DAY(“2003-03-24”);
Output:24
DAYNAME(date) It returns the name of the day from the date. mysql> SELECT DAYNAME(“2019-07-11”);
Output:Thursday
Numeric/Math Functions
Function Description Example with output
POWER(X,Y) Calculates X to the power Y. mysql> SELECT POWER(2,3);
can also be written as
POW(X,Y) Output:8
ROUND(N,D) Rounds off number N to D mysql>SELECT ROUND(2912.564, 1);
number of decimal places.
Note: If D=0, then it rounds Output:2912.6
off the number to the nearest mysql> SELECT ROUND(283.2);
integer.
Output:283
MOD(A, B) Returns the remainder mysql> SELECT MOD(21, 2);
after dividing number A by
number B. Output:1
String Functions
Function Description Example with output
UCASE(string) OR Converts string into uppercase. mysql> SELECT UCASE(“Informatics
UPPER(string) Practices”);
Output: INFORMATICS PRACTICES
LOWER(string) OR Converts string into lowercase. mysql> SELECT LOWER(“Informatics
LCASE(string) Practices”);
Output: informatics practices
MID(string, pos, n) Returns a substring of size n starting from the mysql> SELECT MID(“Informatics”, 3, 4);
OR specified position (pos) of the string. If n is not
specified, it returns the substring from the position
Output: form
SUBSTRING(string,
pos, n) pos till end of the string.
OR mysql> SELECT MID(‘Informatics’,7);
SUBSTR(string, pos, n) Output:atics
LENGTH(string) Return the number of characters in the specified mysql> SELECT LENGTH(“Informatics”);
string.
Output:11
LEFT(string, N) Returns N number of characters from the left side mysql> SELECT LEFT(“Computer”, 4);
of the string.
Output:Comp
RIGHT(string, N) Returns N number of characters from the right mysql> SELECT RIGHT(“SCIENCE”, 3);
side of the string.
Output:NCE
INSTR(string, Returns the position of the first occurrence of the mysql> SELECT INSTR(“Informatics”, “ma”);
substring) substring in the given string. Returns 0, if the
substring is not present in the string. Output: 6

LTRIM(string) Returns the given string after removing leading white


select ltrim(“ Informatics “);
space characters. Output:“Informatics “
RTRIM(string) Returns the given string after removing trailing select rtrim(“ Informatics “);
white space characters. Output: “ Informatics“

TRIM(string) Returns the given string after removing both select rtrim(“ Informatics “);
leading and trailing white space characters. Output: “Informatics“

1. Degree – It refers to the number of columns in a relation


Cardinality – It refers to the number of rows in a relation

2. Domain – A domain is a set of values that can be assigned to an attribute

3. Keys in a Relational Database:


(i) Primary Key :
It is an attribute or set of attributes that uniquely identifies a tuple in a relation.
(ii) Candidate Key:
The attributes of a table which are eligible to be set as primary key are called as
candidate key.
(iii) Alternate Key:
From the list of candidate key one is chosen as primary key and the remaining are
called Alternate key.
(iv) Foreign Key:
▪ A Foreign Key is a field (or collection of fields) in one table that uniquely identifies
a row of another table.
▪ It is used to establish a link between the data in two tables
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 Manipulation Language):
> insert
> update
> delete
(iii) DQL(Data Query Language):
> Select
7. Difference Primary Key and Unique Key

Primary Key Unique Key


Identifies an individual tuple uniquely in a relation
Uniquely identifies each tuple (row) in a table. or table.
Only one primary key per table. Multiple unique keys can exist for a table.
Does not allow NULL values for the column. Allows NULL value for the column.
Keyword used to assign constraint : primary key Keyword used to assign constraint : unique

8: Characteristics of Primary key:


• A primary key uniquely identifies each row (record) within a table.
• It does not allow duplicate entries in a table.
• The primary key attribute(s) cannot contain null values.
• There can be only one primary key attribute in a table.

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.
14. Difference delete and drop:
DELETE Command DROP Command
Data Manipulation Language (DML) command. Data Definition Language (DDL) command.
Used to
(i) remove a table from a database
Used to remove records from a table.
(ii) remove a column or constraint from a table
(iii) remove/delete a database
Space occupied by the table in memory is not
Frees the table space from memory.
freed even if all tuples are deleted.
Syntax
Syntax - To drop a table: DROP TABLE table_name;
DELETE FROM table_name WHERE condition;
- To drop a database: DROP DATABASE
database_name;

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;

(ii) Selecting database:

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

Field DataType Constraint


Roll int Primary key
Name Varchar(20) not null
Age int
DOB date
Marks int default 20
Create table Student(Roll integer primary key, Name varchar(25) not null, Age integer,
DOB date, Marks integer default 20);
(v) To view table structure:
Syntax:
desc tableName; (or) describe tableName;
Eg:
desc student; (or) describe student;

(vi) Inserting records into the table:


Syntax:
Insert into tableName values(col1Value, col2Value,….. ColNValue);
Eg.
Insert into student values(1001, “Hari”, 14, “2009-05-22”, 80);

(vii): Update a record:


Syntax:
Update tableName set colName = newValue where condition;
Eg:
update student set marks = 94 where roll = 1002;
(viii): Delete a record:
Syntax:
Delete from tableName where condition;
Eg:
Delete from student where roll = 1004;

(ix) Add Column:


Syntax:
Alter table tableName add colName datatype constraint;
Eg:
Alter table student add class varchar(5);

(x) Add Primary to existing table:


Syntax:
Alter table tableName add PRIMARY KEY (ColName);
Eg:
Alter table student add primary key (roll);
(xi) Remove Primary Key:
Syntax:
Alter table tableName drop Primary key;
Eg:
Alter table student drop Primary key;
(xii) Remove column:
Syntax:
Alter table TableName drop colName;
Eg:
Alter table Student drop class;
(xiii) Remove/delete table:
Syntax:
Drop table tableName;
Eg:
Drop table Student;
(xiv) View the available tables:
show tables;
(xv): Viewing the table/Querying Table: - Select query
Syntax:
Select * from tableName where condition
group by columnName having groupConditon order by columnName [asc|dsc];

You might also like