Amit Dbms File
Amit Dbms File
Submitted to:
Ms. Ritika Arora
Submitted by:
Name : Amit Kumar
Roll NO. : SG21805
IT Branch(3rd semester)
Topic
Sno. Page No Date Remarks
SQL
SQL is a standard language for accessing and manipulating databases.
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in 1987.
Here are some key points which can be done using SQL:
The CREATE TABLE statement is used to create a new table in a database. The column parameters
specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).
SYNTAX:
The following example creates a table called "Students" that contains five columns: StudentID,
LastName, FirstName, Address, and City:
Output:
Practical-1(b)
INSERTING INFO TO TABLE
2. If you are adding values for all the columns of the table, you do not need to specify the column names
in the SQL query. However, make sure the order of the values is in the same order as the columns in the
table. Here, the INSERT INTO syntax would be as follows:
The following SQL statement inserts a new record in the "Students" table:
Output:
Practical-1(c)
SELECT STATEMENT
The SELECT statement is used to select data from a database. The data returned is stored in a result
table, called the result-set.
Syntax:
Here, column1, column2, ... are the field names of the table you want to select data from. If you want to
select all the fields available in the table, use the following syntax:
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values.
Syntax:
The following SQL statement selects all (including the duplicates) values from the "FirstName" column
in the "Students" table:
Practical-1(e)
DROP TABLE
Syntax:
DROP TABLE table_name;
The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE
values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple
columns (fields).
Syntax:
Syntax:
UPDATE Persons
SET FirstName = 'NURAV'
WHERE PersonID = 3;
Output:
Practical-2(c)
DELETE
Syntax:
DELETE FROM table_name WHERE condition;
The following SQL statement deletes the PERSON “NURAV” from the "Persons" table:
INITIALLY:
AFTER:
Practical-2(d)
ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending
order, use the DESC keyword.
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
The following SQL statement selects all customers from the "Persons" table, sorted by the
"FirstName" column:
INITIALLY:
AFTER:
Practical-2(e)
MIN-MAX
The MIN() function returns the smallest value of the selected column. The MAX() function
returns the largest value of the selected column. Both the functions are very useful to help
determine the range of the data.
SYNTAX:
SELECT MAX(PersonID) AS MaximumPersonID
FROM Persons;
SELECT MIN(PersonID) AS MaximumPersonID
FROM Persons;
Practical-3(a)
Calculating Sum, Average and number of specific entities.
The SUM() function returns the total sum of a numeric column. The function only works on the
numeric columns only. The AVG() function returns the average value of a numeric column and
just like the SUM() function, it only works on the numeric columns only. The COUNT()
function returns the number of rows that matches a specified criterion. This will return the
number of entries in the table which satisfy a given condition.
SYNTAX:
OUTPUT:
SYNTAX:
SYNTAX:
Select Count (name) from student;
OUTPUT :
Practical-3(b)
Using LIKE to modify conditional statements
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
LIKE statements make use of wildcards to modify the conditional statements. There are two
wildcards often used in conjunction with the LIKE operator:
● The percent sign (%) represents zero, one, or multiple characters
● The underscore sign (_) represents one, single character
SYNTAX :
OUTPUT
Practical-3(c)
Using Wildcard characters to modify the conditional statement
SYNTAX:
Select * from student where Name like '%a_';
OUTPUT:
Practical-3(d)
Specifying multiple values in the conditional statement
The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a
shorthand for multiple OR conditions, which means rather than using multiple OR conditions,
we can use IN operator.
SYNTAX :
Select * from student where Stream in('CSE', 'IT');
OUTPUT:
Practical-3(e)
Getting values of records within a set range
The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates. The BETWEEN operator is inclusive which means that beginning and
ending values are included. It also sorts the values in ascending order by default.
SYNTAX :
Select * from student where Roll_no between 21124 and 21854;
Output :
Practical 4
Joining tables in MySQL
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them. There are 4 ways to join the tables in MySQl and we will talk about them briefly
here. To understand the concept of joining, let's consider these two tables :
Table: Student
Table: Fee
INNER JOIN:
The INNER JOIN keyword selects records that have matching values in both tables. It takes the
values that are in the intersection of both tables and display them as per the user instructions.
The LEFT JOIN allows you to query data from two or more tables. Similar to the INNER JOIN
clause, the LEFT JOIN is an optional clause of the SELECT statement, which appears
immediately after the FROM clause.
RIGHT JOIN:
The RIGHT JOIN keyword returns all records from the right table (books), and the matching
records (if any) from the left table (student). MySQL RIGHT JOIN is similar to LEFT JOIN,
except that the treatment of the joined tables is reversed.