Unit 2 - DATABASE CONCEPTS AND SQL 2
Unit 2 - DATABASE CONCEPTS AND SQL 2
Database concepts
and the
SQL
Data - it can be of any form (numbers, text, graphs, pictures etc)
A file can be understood as a container to store data in a computer.
Alternate Key - The Candidate Key that is not the primary key
CHAR VARCHAR
stores characters of fixed length. stores characters of variable size.
is used when the length of data is is used when the length of data is
known so that we declare the field unknown.
with the same length.
could be any value from 0 to 255 could be any value from 0 to 65535
To create a Database
CREATE DATABASE db_name;
To show all existing Databases
SHOW DATABASES;
To use a Database / open an existing database
USE db_name;
To show all existing Tables
SHOW TABLES;
To create a Table
CREATE TABLE table_name
(
Col1 datatype(size) constraint,
Col2 datatype(size) constraint,
:
Coln datatype(size) constraint
);
E.g. CREATE TABLE Student
(
RNo int Primary Key,
Name varchar(12),
Class varchar(5),
House varchar(10),
DOB Date
);
Note:
Jupiter
SELECT DISTINCT House FROM Student;
Saturn
SELECT ALL House FROM Student;
(Includes duplicates)
Where Clause
The WHERE clause is used to retrieve data that meet some
specified conditions
SELECT * FROM EMPLOYEE WHERE Salary > 5000 AND
DeptId = 'D04';
Q. display name and department number of all those employees who are
earning salary between 20000 and 50000 (both values inclusive).
DUAL
DUAL is 1 row, 1 column table present by default in all Oracle
databases.
5*10
SELECT 5*10;
50
UPDATE
The UPDATE statement is used to make such modifications in the
existing data.
UPDATE table_name
SET Col1 = val1, Col2 = val2, …
WHERE condition;
3
SELECT MOD(11,4) Modulus;
1.5
POWER( ) / POW( )
n th
This function returns m . i.e. a number m raised to the n power.
9
SELECT POW(3,2) Result;
ROUND( )
This function returns a number rounded off as per given instructions
Result
SELECT ROUND(325.415, -2) Result;
300
Note: the `ROUND` function with a negative second argument rounds the number to
the left of the decimal point. In this case, -1 specifies rounding to the nearest ten's
place. Since 15.193 is closer to 20 than 10, the rounded result is 20.
Q. Round off value 15.193 to 2 decimal places Result
UCASE( ) / UPPER( )
Result
This function converts the given string into uppercase.
SELECT UPPER(‘Hello’) Result; HELLO
SELECT UCASE(‘Hello’) Result;
LCASE( ) / LOWER( )
This function converts the given string into lowercase. Result
LENGTH( str)
length
SELECT LENGTH(‘WELCOME’) length;
7
COME
LEFT( )
This function returns the leftmost number of characters as specified.
Returns NULL if the argument is NULL.
Result
LEFT(str, len)
SELECT LEFT(‘WELCOME’, 4) Result; WELC
INSTR( )
This function searches for a substring in a given string and returns the
position of its first occurrence.
Result
INSTR(str, substr)
6
SELECT INSTR(‘WELCOME’, ‘ME’) Result;
Name Result
Meera 0
SELECT Name, INSTR(Name, ‘at’) AS Result
Pratyush 3
FROM Student;
Sathya 2
LTRIM( )
This function removes leading spaces from the left of a given string.
LTRIM(str) Result
RTRIM( )
This function removes trailing spaces from the right of a given string.
RTRIM(str) Result
WELCOME###
SELECT TRIM(‘EL’ FROM ‘WELCOME’) AS Result;
Result
SELECT TRIM(both ‘#’ FROM ‘### WELCOME###’) AS ‘Result’;
WELCOME
DATE FUNCTIONS
SELECT CURDATE( );
DATE( )
This function extracts the date part of a date or datetime Result
expression.
2022-12-31
SELECT DATE(‘2022-12-31 01:02:03’ ) AS Result;
MONTH( )
Result
This function returns the month from the date argument,
12
in the range 1 to 12
MONTHNAME( )
Result
This function returns the month from the date argument
September
SELECT MONTHNAME(‘2022-09-31’ ) AS Result;
YEAR( )
Result
This function returns the year from the date
argument, in the range 1000 to 9999 2022
DAY( )
Result
This function returns day part of the date.
29
SELECT DAY(‘2022-09-29’ ) AS Result;
DAYNAME( )
Result
This function returns name of the day of a week.
Wednesday
SELECT DAYNAME(‘2024-08-07’) AS Result;
NOW( )
Result
This function returns the current date and
time.("YYYY-MM-DD HH-MM-SS") 2024-08-07 19:14:08
SELECT NOW( ) AS Result;
AGGREGATE FUNCTIONS
MAX( )
MIN( )
AVG( )
This function returns the average of values from a given column or
expression.
SELECT AVG(MARKS) “Average”
FROM Student;
COUNT( )
10 3 8750.00
14 5 10885.00
20 4 12678.00
24 6 9400.00
Q. To calculate the number of employees in each department
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
Table : Product Table : Discount
id prod_name id dis_percent
1 Iwatch 2 20
2 IPhone 4 17
3 IPod
4 macbook
id prod_name id dis_percent
SELECT * 2 IPhone 2 20
FROM Product, Discount
4 macbook 4 17
WHERE Product.id = Discount.id;
● Equi join in SQL can be written with WHERE as well as
ON clause.
SELECT *
FROM CAR_SALES
ORDER BY QT2 DESC;
a. SELECT LEFT(COLOR, 3) FROM Cars WHERE
COLOR='Blue' OR COLOR='Black' OR COLOR='Brown';
c. Number of tuples: 6
Primary key column: CarID
I. SELECT SUBSTRING('Database Management System', 10, 6);
SELECT *
FROM BLOCKCHAIN
WHERE MONTH(transaction_date) = 5;