Informatics Practices - Import Theory Question - SQL
Informatics Practices - Import Theory Question - SQL
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
TRIM(string) Returns the given string after removing both select rtrim(“ Informatics “);
leading and trailing white space characters. Output: “Informatics“
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.
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.
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