0% found this document useful (0 votes)
19 views55 pages

Dbmslab

SQL is a standard programming language for managing relational databases. It was first developed in the 1970s at IBM and was later adopted as the standard. SQL has four main components: DDL for defining database structure, DML for manipulating data, DCL for controlling access privileges, and TCL for managing transactions.

Uploaded by

Parikshit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views55 pages

Dbmslab

SQL is a standard programming language for managing relational databases. It was first developed in the 1970s at IBM and was later adopted as the standard. SQL has four main components: DDL for defining database structure, DML for manipulating data, DCL for controlling access privileges, and TCL for managing transactions.

Uploaded by

Parikshit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Q1 explain sql and its history

SQL (Structured Query Language) is a standard programming language for managing and manipulating
relational databases

10 uses of SQL
There are a variety of uses of SQL, although the frequency of these uses may change based on your
specific career. For example, data scientists and web development professionals both use this language,
yet they use it for different purposes. You can use SQL to:

1. Alter data within a table

If you access a database that has organized data within a table, you can use SQL to manipulate this data.
For example, you can change certain data points within the table. Additionally, you could alter every
section of the table with updated data. This is a very common use of SQL.

2. Create a table

Another use of SQL is the ability to create a new table. Users may create a brand new database by adding
new data into a table once it's created. This function allows users to contribute data to the system, which
can further analyze, manipulate and store it.

3. Retrieve data

Many data scientists can use SQL to retrieve data from within the database. The process for this in SQL is
DQL, which stands for Data Query Language. After retrieving the data, it's easier to analyze and assess
it. The syntax for this command is "SELECT."

4. Change data structure

Another use of SQL is to change the structure of the data. You can do this by changing the database or
table where the data's stored. A professional may use SQL for this purpose if there's new data that makes
the current storage process outdated.

5. Define the database's schema

You can use SQL to define your database's schema, which contributes to data descriptions and
structuring. The name of this process is data definition language, or DDL. The commands you can use for
this process include "CREATE," "DROP," "ALTER," "TRUNCATE," "COMMENT" and "RENAME."

6. Manipulate the data

You can use the DML, or data manipulation language, feature to store, modify, delete or update the data.
You might use SQL for this purpose if you want to refresh the whole database to ensure it's accurate and
updated. The commands for this include "INSERT," "UPDATE" and "DELETE." Here's an example of
how to use these commands:

1
7. Grant access to data

If you manage the database, you can provide access to other users using SQL. The method for this is
DCL, which means data control language. You can use the commands "GRANT" and "REVOKE" to
determine which users have access to which databases within the program. This use may benefit
businesses that store confidential or sensitive data within the program.

8. Define user functions and procedures

Using SQL, you can add functions and procedures based on your specific database needs. For example,
you can link simple SQL syntax to a specific command before you begin using the system. This helps you
meet the requirements for your business and simplifies the process of using the language.

9. Analyze data manually

You can use SQL to manually analyze the data within your system. This may be necessary when you
need to intervene with the system manually. While troubleshooting or solving an issue with the database,
manual analysis can provide an alternative way to continue accessing the data. Users can use SQL queries
to search for necessary data, which they can then use for analysis.

10. Combine datasets

The SQL Join function allows users to combine data from two sets. You might use this feature if you
want to combine and compare two different tables. You can use the following commands:

(INNER) JOIN: This command can show the programmer if any outputs in the two systems are
overlapping. For example, if there are multiple outputs classified as "REVIEW," this function shows each
of these outputs.

LEFT (OUTER) JOIN: This command shows the user every aspect of the left-hand table, and compares it
to the outputs of the other table.

RIGHT (OUTER) JOIN: This command shows the user every aspect of the right-hand table. Then, it
compares these aspects with the outputs of the left-hand table.

FULL (OUTER) JOIN: This method completely joins the two tables together. It can show a user all the
areas where data matches between the two tables.

History

SQL was first developed in the early 1970s at IBM by a team led by Donald D. Chamberlin and Raymond
F. Boyce. The original name of the language was SEQUEL, which stood for "Structured English Query
Language." SEQUEL was designed to be a simple and intuitive way to access and manipulate data
stored in IBM's System relational database management system. SEQUEL was later renamed to SQL,
and IBM released the first commercial implementation of the language in 1981 as part of their System

2
relational database management system (RDBMS).

SQL quickly became the standard language for interacting with relational databases, and it was soon
adopted by other RDBMS vendors such as Oracle, Sybase, and Microsoft. SQL also saw the emergence
of several popular open-source implementations of the language, such as MySQL and PostgreSQL.
Oracle and Microsoft's implementations and commercial offerings are widely used today.

Q2 Discuss the DBMS languages and elaborate it with its data types
1. Data Definition Language (DDL)

DDL stands for Data Definition Language. It is used to define database structure or pattern.

It is used to create schema, tables, indexes, constraints, etc. in the database.

Using the DDL statements, you can create the skeleton of the database.

Data definition language is used to store the information of metadata like the number of tables and
schemas, their names, indexes, columns in each table, constraints, etc.

Here are some tasks that come under DDL:

 Create: It is used to create objects in the database.

 Alter: It is used to alter the structure of the database.

 Drop: It is used to delete objects from the database.

 Truncate: It is used to remove all records from a table.

 Rename: It is used to rename an object.

 Comment: It is used to comment on the data dictionary.

These commands are used to update the database schema that's why they come under Data definition
language.

2. Data Manipulation Language (DML)

DML stands for Data Manipulation Language. It is used for accessing and manipulating data in a
database. It handles user requests.

Here are some tasks that come under DML:

3
Select: It is used to retrieve data from a database.

 Insert: It is used to insert data into a table.

 Update: It is used to update existing data within a table.

 Delete: It is used to delete all records from a table.

 Merge: It performs UPSERT operation, i.e., insert or update operations.

 Call: It is used to call a structured query language or a Java subprogram.

 Explain Plan: It has the parameter of explaining data.

 Lock Table: It controls concurrency.

3. Data Control Language (DCL)

DCL stands for Data Control Language. It is used to retrieve the stored or saved data.

The DCL execution is transactional. It also has rollback parameters.

(But in Oracle database, the execution of data control language does not have the feature of rolling back.)

Here are some tasks that come under DCL:

 Grant: It is used to give user access privileges to a database.

 Revoke: It is used to take back permissions from the user.

 There are the following operations which have the authorization of Revoke:

CONNECT, INSERT, USAGE, EXECUTE, DELETE, UPDATE and SELECT.

4. Transaction Control Language (TCL)

TCL is used to run the changes made by the DML statement. TCL can be grouped into a logical
transaction.

Here are some tasks that come under TCL:

 Commit: It is used to save the transaction on the database.

 Rollback: It is used to restore the database to original since the last Commit.

4
Data types in SQL Server are organized into the following categories:

 Numeric Data Types

 String Data Types

Numeric Data Types

 bigint - Allows whole numbers between -9,223,372,036,854,775,808 and


9,223,372,036,854,775,807

 numeric -Allows numbers from -10^38 +1 to 10^38 –1

 bit - Integer that can be 0, 1, or NULL

 smallint - Allows whole numbers between -32,768 and 32,767

 decimal - Allows numbers from -10^38 +1 to 10^38 –1.

 smallmoney -Monetary data from -214,748.3648 to 214,748.3647

 int - Allows whole numbers between -2,147,483,648 and 2,147,483,647

 tinyint - Allows whole numbers from 0 to 255

 money - Monetary data from -922,337,203,685,477.5808 to 922,337,203,685,477.5807

 float - Floating precision number data from -1.79E + 308 to 1.79E + 308.

 bigint - Allows whole numbers between -9,223,372,036,854,775,808 and


9,223,372,036,854,775,807

String Data Types

 char(n) - Fixed width character string

 varchar(n) - Variable width character string

 varchar(max) - Variable width character string

 text - Variable width character string

 nchar- Fixed width Unicode string

 nvarchar - Variable width Unicode string

5
 nvarchar(max) - Variable width Unicode string

 ntext - Variable width Unicode string

 binary(n) - Fixed width binary string

 varbinary - Variable width binary string

 varbinary(max) - Variable width binary string

 image - Variable width binary string

6
EXPERIMENT-1
Objective- to create,select, insert and display the values in a database.
Create Command

Syntax- CREATE TABLE table_name ( column1 datatype,column2


datatype, column3 datatype, ....);

7
Insert Command

2. Insert- INSERT INTO table_name (column1, column2, column3, ...) VALUES


(value1, value2, value3, ...);

Select Command
Syntax- Select* from table_name

8
EXPERIMENT-2
Objective- to add,modify,drop and display the values in a database.

Alter(add) Command
Syntax- ALTER TABLE table_name ADD column_name datatype;

9
Alter(modify)
Synatx-ALTER TABLE table_name MODIFY COLUMN column_name datatype;

Alter(drop) Command
Syntax-ALTER TABLE table_name DROP COLUMN column_name;

10
EXPERIMENT-3
Objective- to update,delete,Distinct and display the values in a database
Update Command
Syntax- UPDATE table_name SET [column_name1= value1,... column_nameN =
vaueN] [WHERE condition]

Delete Command
Syntax- DELETE FROM table_name [WHERE condition];DELETE FROM table_name

11
Distinct Command
Syntax-SELECT DISTINCT expressions FROM tables [WHERE conditions];

12
EXPERIMENT-4
Objective- to use and,or,not and display the values in a database
And Command
Syntax-SELECT column1, column2,..FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

13
NOT Command
Synatx-SELECT column1, column2,FROM table_name
WHERE NOT condition;

14
EXPERIMENT-5
Objective- to use orderby and display the values in a database

15
Orderby Command
Syntax-SELECTcolumn1, column2,FROM table_name ORDER BY column1,
column2, ... ASC|DESC;

EXPERIMENT-6
Objective- to use SQL Functions and display the values in a database

16
Maximum Command
Syntax- Select max(columnname)from table_name

Minimum Command
Syntax- Select min(columnname)from table_name

17
Count Command
Syntax- Select count(columnname)from table_name

18
Average Command
Syntax- Select avg(columnname)from table_name

Sum Command
Syntax- Select sum(columnname)from table_name

19
EXPERIMENT-7
Objective- to use LIKE Functions and display the values in a database
Syntax- Select* from table_name where(columnname)like’a%’

20
21
Syntax- Select* from table_name where(columnname)like’%l’

Syntax- Select* from table_name where(columnname)like’%ul%’

22
Syntax- Select* from table_name where(columnname)like’_u%’

Syntax- Select* from table_name where(columnname)like’p__%’


23
Syntax- Select* from table_name where(columnname)like’p%l’

Syntax- Select* from table_name where(columnname)notlike’a%’

24
EXPERIMENT-8
Objective- Create a table for a customer who orders products from different
countries. Find the

25
a) the distinct value

26
b)values where the customer orders from Europe, Mexico countries

c)values where the customer orders from Germany and Berlin

d)the customer order from Germany or Mexico

e) the customer orders not from Germany

27
f)the customer ordered from Germany but not from Mexico

28
g)the customer orders from different countries display the result n ascending
order of different countries

29
EXPERIMENT-9
Objective- to use JOIN Functions and display the values in a database
Syntax- ⁠SELECT * FROM TABLE1 INNER JOIN TABLE2 ON
TABLE1.COLUMNNAME=TABLE.COLUMNNAME

Syntax- ⁠SELECT COLUMNNAME FROM TABLE1 LEFT JOIN TABLE ON


TABLE1.COLUMNNAME=TABLE2.COLUMNNAME

Syntax-⁠ ⁠SELECT COLUMNNAME FROM TABLE1 RIGHT JOIN TABLE2 ON


TABLE1.COLUMNAME=TABLE2.COLUMNNAME

30
Syntax-⁠ ⁠SELECT COLUMNNAME FROM TABLE1 FULL OUTER JOIN TABLE2 ON
TABLE1.COLUMNNAME=TABLE2.COLUMNNAME

31
EXPERIMENT-10
Objective- to use ALIASES Functions and display the values in a database
Syntax- ⁠ SELECT COLUMNNAME AS C_NAME FROM TABLENAME

32
EXPERIMENT-11
Objective- to use UNION OPERATOR Functions and display the values in a
database

Syntax- SELECT COLUMNANME FROM TABLE1 UNION SELECT COLUMNNAME


FROM TABLE2

Syntax- SELECT COLUMNANME FROM TABLE UNION ALL SELECT COLUMNNAME


FROM TABLE2

33
Syntax- ⁠SELECT COLUMNANME FROM TABLE1 MINUS SELECT COLUMNNAME
FROM TABLE2

Syntax- S⁠ ELECT COLUMNANME FROM TABLE1 INTERSECT SELECT


COLUMNNAME FROM TABLE2

34
EXPERIMENT-12
Objective - Write a query in SQL to display the last name and job tle of all
employees who do not have a manager

EXPERIMENT-13
Objective- Write a query in SQL to display the last name, salary, and
commission of all employees who earn commissions. Sort data in descending order
of salary and commissions

35
36
EXPERIMENT-14
Objective- ⁠ Write a query in SQL that prompts the user for a manager ID and ⁠
generates the employee ID, last name, salary, and department for that manager’s
employees. The HR department wants the ability to sort the report on a selected
column

37
EXPERIMENT-15
Objective- ⁠ Write a query in SQL to Display all employee last names in
which ⁠ the third le er of the name is a.

38
EXPERIMENT-16
Objective- Write a query in SQL to Display the last name of all employees ⁠ who
have both an a and an e in their last name.

39
EXPERIMENT-17
Objective- Write a query in SQL to Display the last name, job, and salary for ⁠ all
employees whose job is sales representa ve or stock clerk and whose salary is not
equal to $2,500, $3,500, or $7,000.

40
EXPERIMENT-18
Objective- Write a query in SQL to display the employee number, last name,
salary, and salary increased by 15.5% (expressed as a whole number) for each
employee. Label the column New Salary.

41
EXPERIMENT-19
Objective- Create a report that produces the following for each
employee: <employee last name> earns <salary> monthly but wants <3
times salary>. Label the column Dream Salaries.

42
EXPERIMENT-20
Objective- Create a query to display the last name and salary for all
employees. Format the salary to be 15 characters long, left-padded with the $
symbol. Label the column SALARY

43
EXPERIMENT-21
Objective- ⁠Display each employee’s last name, hire date, and salary
review date, which is the first Monday after six months of service. Label the
column REVIEW. Format the dates to appear in the format similar to
“Monday, the Thirty-First of July, 2000.”

44
EXPERIMENT-22
Objective- Display the last name, hire date, and day of the week on
which the employee started. Label the column DAY. Order the results by the day
of the week, starting with Monday.

45
46
EXPERIMENT-23
Objective- WRITE A QUERY IN PLISQL TO PRINT 'HELLO WORLD'
SYNTAX: DECLARE message
World!'; BEGIN
DBMS OUTPUT.PUT LINE(message);
END;

47
EXPERIMENT-24
Objective- write a query in pl/sql to find the sum of two
variables

48
EXPERIMENT-25
Objective- write a query in pl/sql to demonstrate the use of
comments
SYNTAX: DECLARE
— variable declaration
Message 'Demonstration of comments'; BEGIN

• PL/SQL executable statements(s)

dbms output.put line(message);


END;

49
EXPERIMENT-26
Objective- write a query in pl/sql to take input from the
user end.
SYNTAX:i integer;
j integer; s
integer; begin

dbms output.put_line('sum of' lill' and 'lljll' is 'IIS); end;

50
EXPERIMENT-27
Objective- write a query in pl/sql for the use of loops

SYNTAX:DEcLARE
VARI NUMBER;
BEGIN

FOR VAR2 IN 1..5


LOOP DBMS OUTPUT.PUT LINE (VARI+VAR2); END LOOP; END;

51
EXPERIMENT-28
Objective- write a query to demonstrate the use of if-else
statement.
SYNTAX:DECLARE
Marks 55;
BEGIN

F marks > 45 then dbrns output.put line('Congratulations! You pass the


Examination');

ELSE dbms are Failed! Better Luck Next Time');


END IF; END

52
EXPERIMENT-29
Objective- Execute the program in plSQL to calculate sum, multiplication of 3
numbers

DECLARE
num1 NUMBER := 10; -- Example value for the first variable
num2 NUMBER := 20; -- Example value for the second variable
num3 NUMBER := 30; -- Example value for the third variable
add NUMBER;
product NUMBER;
BEGIN
add := num1 + num2 + num3;
product := num1 * num2 * num3;
DBMS_OUTPUT.PUT_LINE('The sum of ' || num1 || ' , ' || num2 || ' and ' ||
num3 || ' is: ' || add);
DBMS_OUTPUT.PUT_LINE('The product of ' || num1 || ' , ' || num2 || ' and
' || num3 || ' is: ' || product);

53
EXPERIMENT-30
Objective- Execute the program in pl SQL to swap the 2
variables.
DECLARE
num1 NUMBER := 10; -- Example value for the first variable
num2 NUMBER := 20; -- Example value for the second variable
BEGIN
DBMS_OUTPUT.PUT_LINE('Before Swapping: Num 1 = ' || num1 || ' and
Num 2 = ' || num2);
num1 := num1 + num2;
num2 := num1 - num2;
num1 := num1 - num2;
DBMS_OUTPUT.PUT_LINE('After Swapping: Num 1 = ' || num1 || ' and Num
2 = ' || num2);
END;

54
55

You might also like