Unit 4 Bcomcardbms 2024
Unit 4 Bcomcardbms 2024
introduction to SQL
SQL is used to query, insert, update, and delete data stored in relational database
management systems (RDBMS) like MySQL, PostgreSQL, Microsoft SQL Server,
Oracle, and SQLite.
1
Basic SQL Operations
Data Querying
Data Definition Language(DDL)
Data Manipulation Language(DML)
Data Control Language(DCL)
Syntax
SELECT column1,
column2, ... FROM
table_name WHERE
condition;
Example 1
SELECT * FROM Employees;
Output
2
Example 2 : Selecting Specific Columns
DDL (Data Definition Language) is used to define and manage the structure of database
objects like tables,
3
It includes commands such as
CREATE a table,
ALTER a table,
DROP a table,
TRUNCATE a table,
RENAME a table.
CREATE a table
Syntax
Example 1
Table created
Output
Table student
ID Name Address
Example 2
4
SQL>CREATE TABLE employee ( empID NUMBER(6) , Name VARCHAR2(100),
Address NUMBER(10 );
Table created
Table employee
Primary keys must contain UNIQUE values, and cannot contain NULL values.
Syntax
Example 1
CREATE TABLE Products ( Product_ID NUMBER(4) PRIMARY KEY, Product_Name
VARCHAR2(200), Price NUMBER(8) );
OUTPUT
Table :Products
Product_ID Product_Name Price
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.
Syntax
CREATE TABLE child_table ( column1 datatype, column2 datatype REFERENCES
parent_table (parent_column) );
Table1 : Students
CREATE TABLE Students ( StuID NUMBER(5) PRIMARY KEY, Name
VARCHAR2(50), Age NUMBER(2) );
output
StuID Name Age
1 raj 17
2 ravi 18
5 rakesh 19
Table 2 stumarks
CREATE TABLE stumarks (Course_Name VARCHAR2(50), StuID NUMBER(5)
REFERENCES Students (Stu_ID),
output
Course_Name StuID
BCA 1
BCOM 2
BCOM(CA) 5
Alter a table
6
Syntax
ALTER TABLE table_name
ADD column_name datatype;
output
ID Name Address Email
Syntax
ALTER TABLE table_name DROP COLUMN column_name;
ID Name Address
Drop aTable
Syntax
7
Example1
DROP TABLE Shippers;
Example2
DROP TABLE employee;
Example 3
The TRUNCATE TABLE statement is used to delete the data inside a table, but not the
table itself.
Syntax
TRUNCATE TABLE table_name;
8
Example 2: Truncate the Employees Table
Rename
The RENAME statement is used to change the name of a table or a column in SQL.
Syntax
9
Example 2 old table name : students
DML is used for managing and manipulating data within database objects (tables). It
includes the following commands:
Insert
The INSERT statement in SQL is used to add new records (rows) to a table. You can
insert data into specific columns or all columns in a table.
10
Syntax
INSERT INTO <tablename> VALUES (value1, value2, value3, ...);
Example 1
Output
Example 2
Output
Example 3
Table created
11
INSERT INTO library_books VALUES(1, 'The Alchemist', 'Paulo Coelho');
INSERT INTO library_books VALUES(2, '1984', 'George Orwell');
INSERT INTO library_books VALUES( 3, 'The Great Gatsby', 'F. Scott Fitzgerald');
NSERT INTO library_books Values(4, 'The Hobbit', 'J.R.R. Tolkien');
Output
Update
The UPDATE statement in SQL is used to modify existing records in a table. It allows
you to change one or more columns for specific rows based on conditions.
Syntax
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
example 1
12
UPDATE students SET grade = 'A+' WHERE name = 'Bob';
UPDATE students SET age = 18, grade = 'B' WHERE name = 'Charlie';
Output
13
DELETE
The DELETE statement in SQL is used to remove existing records from a table. You
can delete specific rows based on a WHERE condition or remove all records from a
table.
Syntax:
Example 1
14
Delete Multiple Rows Based on Condition
Example 2
Output
15
DELETE FROM employees WHERE department = 'IT';
Output
Select
The SELECT statement in SQL is used to retrieve data from one or more tables.
It allows you to choose specific columns, filter rows, and even perform calculations or
aggregations on the data.
Syntax
Example 1
16
Select All Columns
17
Example2
18
Select with Condition
Get details of students with grade 'B'.
Data Control Language (DCL) is a subset of SQL commands that deals with rights,
permissions, and access control in a database. It is used to grant or revoke user
privileges and control how users interact with the database.
DCL Commands:
19
Syntax
Example1
Example2
Allow user John to read and insert data into the employees table.
Example 1
Example 2
Example3
20
Explain Additional Select Query keywords and Queries
Example
Query1
Output
21
Using BETWEEN
Query
SELECT name, age FROM students WHERE age BETWEEN 18 AND 20;
Using IN
List students who live in either Chicago or Miami.
Query
Output
22
Using LIKE for Pattern Matching
Query
Order BY
Query
Group By
The GROUP BY clause groups rows that have the same values in specified columns and
is often used with aggregate functions like COUNT(), SUM(), AVG(), MIN(), or
MAX().
23
Syntax:
GROUP BY — Groups rows that share the same value in one or more columns.
Example
24
****** Additional Select Queries and Keywords end************
Relational Set Operators in SQL are used to combine the results of two or more SELECT
queries. These operators follow set theory principles (like in mathematics) and help manage data
across multiple tables.
Union
Union ALL
Intersect
Minus or Except
Union
The UNION operator in SQL is used to combine the results of two or more SELECT
statements into a single result set. It removes duplicate records by default.
Syntax
UNION
Both SELECT statements must have the same number of columns with similar data
types.
Example
25
SELECT name, course FROM students_science
UNION
UNION
26
SELECT name, course FROM Bcom(CA);
Output
Union ALL
The UNION ALL operator is used to combine the results of two or more SELECT
statements including duplicates.
Syntax
UNION ALL
27
SELECT column1, column2, ... FROM table2;
UNION ALL
Output
UNION ALL
28
SELECT name, course FROM students_arts;
Output
Intersect
The INTERSECT operator returns common rows from two or more SELECT queries.
Syntax:
INTERSECT
Both SELECT statements must have the same number of columns with compatible
data types.
INTERSECT
29
Output
30
Output
Minus or except
The EXCEPT (or MINUS )operator returns rows from the first SELECT query that are
not present in the second SELECT query.
Syntax
EXCEPT
Both SELECT statements must have the same number of columns with
compatible data types.
Duplicates are automatically removed from the final result.
Sample Tables:
31
Find Math Students Not Enrolled in Science
EXCEPT
Output
Example 2
Output
32
Explain Additional Data Definition Commmands
Data Definition Language (DDL) commands are used to define, modify, and manage
database structures like tables, schemas, and indexes.
Syntax
33
TRUNCATE — Remove All Data but Keep Table Structure
34