Dbms 1
Dbms 1
No:
1)Creation, altering and droping of tables and inserting rows into a table (use constraints
while creating tables) examples using SELECT command.
1. DDL (Data Definition Language):DDL commands are used to define and modify the
structure of database objects. This includes creating, altering, and deleting databases,
tables, indexes, and other database elements.
2. DQL (Data Query Language):DQL commands are used to retrieve data from the database.
The primary DQL command is:
a)SELECT: Used to query and retrieve data from tables.
Syntax: SELECT column1, column2, ... FROM table_name WHERE condition ORDER BY
column_name;
Eg: select * from Student;
Output:
EMPLOYEEID FIRSTNAME LASTNAME AGE
------------------- ---------------- ----------------- ----------
1 Sasra Jupelli 19
2 Veda Rohan 18
3 Rishi Karanam 18
4 Mahesh Babu 22
5 Kalyani Tamatam 25
C)ORDER BY: Sorts the result in ascending (ASC) or descending (DESC) order.
EG: SELECT F_NAME, Marks FROM Stud ORDER BY Marks DESC LIMIT 2;
----------------------- ----------
CSE Dept DBMS
Laboratory
Date: Exp No: Page
No:
balani 100
Keera 99
D)GROUP BY: Groups rows with the same values and performs aggregate functions.
8 rows selected.
EG: SQL> SELECT Marks, COUNT(*) FROM Stud GROUP BY Marks HAVING COUNT(*) > 1;
3. DML (Data Manipulation Language):DML commands are used to manipulate the data
within database tables. This involves inserting, updating, and deleting data.
Key DML commands:
1 Sasra Jupelli 19
2 Veda Rohan 18
3 Rishi Karanam 18
4 Mahesh Babu 22
5 Kalyani Tamatam 25
1 Sasra Jupelli 19
2 Veda Rohan 22
3 Rishi Karanam 18
4 Mahesh Babu 22
5 Kalyani Tamatam 25
1 Sasra Jupelli 19
2 Veda Rohan 18
3 Rishi Karanam 18
5 Kalyani Tamatam 25
CSE Dept DBMS
Laboratory
Date: Exp No: Page
No:
4. DCL (Data Control Language):DCL commands are used to control access to data within
the database. This involves granting and revoking permissions to users.
Key DCL commands:
RESULT: Hence to Creation, altering and droping of tables and inserting rows into a table is
executed successfully.
2) Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS, UNION,
INTERSET, Constraints. Example:- Select the roll number and name of the student who
secured fourth rank in the class.
Description
This query retrieves the roll number and name of the student who secured the 4th rank in the
class by joining the teacher and results tables.
4.USING IN CLAUSE:
SYNTAX: SELECT column_name FROM table_name WHERE column_name IN (value1,
value2, ...);
EXAMPLE: select stu_id from results where rank in (1,2,3);
OUTPUT:
STU_ID
----------
2
3
RESULT: Hence Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS,
UNION, INTERSET, Constraints. Example:- Select the roll number and name of the student
who secured fourth rank in the class is executed successfully.
3) Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN), GROUP BY,
HAVING and Creation and dropping of Views.
Aggregate functions perform calculations on a set of values and return a single value.
SUM(MARKS)
----------
385
OUTPUT:
STNAME STAGE
----- ----
kumar 20
mansoor 19
vedit 18
rishi 20
rohit 16
5 rows selected.
1.Conversion functions
To_char: Converts a number or date to a string.
Syntax: TO_CHAR(value, format)
Example: SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') AS formatted_date
FROM DUAL;
Output:
FORMATTED_DATE
-----------------------------
06-MAR-2025 17:07:04
1. String Functions
Concatenation (||): Combines two or more strings.
CSE Dept DBMS
Laboratory
Date: Exp No: Page
No:
Syntax: string1 || string2
Example: SELECT 'lighter' || ' Princess' AS greeting FROM DUAL;
Output:
GREETING
-----------
Lighter Princess
LPAD: Pads a string from the left with a specified character.
Syntax: LPAD(string, length, pad_char)
Example: SELECT LPAD('535', 5, '0') AS padded_string FROM DUAL;
Output:
PADDED_STRING
--------------
00535
3.Date Functions
SYSDATE: Returns the current system date and time.
Syntax: SYSDATE
Example: SELECT SYSDATE FROM DUAL;
Output:
SYSDATE
-----------
06-MAR-25
NEXT_DAY: Returns the next occurrence of a given day of the week.
Syntax: NEXT_DAY(date, 'day')
NEXT_MONDAY
-----------
10-MAR-25
ADD_MONTHS: Adds a number of months to a date
Syntax: DD_MONTHS(date, number_of_months)
Example: SELECT ADD_MONTHS(SYSDATE, 3) AS future_date FROM DUAL;
Output:
FUTURE_DATE
-----------
05-JUN-25
LAST_DAY: Returns the last day of the month for a given date.
Syntax: LAST_DAY(date)
Example: SELECT LAST_DAY(SYSDATE) AS last_day FROM DUAL;
Output:
LAST_DAY
-----------
31-MAR-25
MONTHS_BETWEEN: Returns the number of months between two dates.
Syntax: MONTHS_BETWEEN(date1, date2)
Example: SELECT MONTHS_BETWEEN('01-JUN-2025', '01-MAR-2025') AS
months_diff FROM DUAL;
Output:
MONTHS_DIFF
------------
3
LEAST: Returns the smallest value among given values.
Syntax: LEAST(value1, value2, ...)
Example: SELECT LEAST(10, 20, 55, 30) AS min_value FROM DUAL;
Output:
MIN_VALUE
---------
10
GREATEST: Returns the largest value among given values.
Syntax: GREATEST(value1, value2, ...)
Example: SELECT GREATEST(10, 60, 5, 30) AS max_value FROM DUAL;
Output:
MAX_VALUE
---------
60
TRUNC: Truncates a date or number to a specific unit.
Syntax: TRUNC(value, unit)
Example: SELECT TRUNC(SYSDATE, 'MM') AS truncated_date FROM DUAL;
CSE Dept DBMS
Laboratory
Date: Exp No: Page
No:
Output:
TRUNCATED_DATE
-----------
01-MAR-25
ROUND: Rounds a date or number.
Syntax: ROUND(value, unit)
Example: SELECT ROUND(SYSDATE, 'MM') AS rounded_date FROM DUAL;
Output:
ROUNDED_DATE
-----------
01-MAR-25
RESULT: Hence to Implement queries using conversion functions ,
stringfunctions ,datafunction is executed successfully.