dbms
dbms
Figure 1 A
primary key
It is the unique identifier for each row in a table. There can only be one
primary key in a table, and it can't be null. ID is the primary key for the
above table. Each student is uniquely identified by their ID. Now two rows
have the same ID. The first column is usually the primary key of the table.
(Refer Figure 1A)
Foreign Key
A foreign key is a column or a set of columns in a table that refers to the
primary key of another table. It establishes the relationship between the
table. A foreign key can be null and there can be more than one foreign key
in the table. For example, in the Department table you might have an emp_id
column as a foreign key. This allows you to link departments to specific
employees.
2. UPDATE statement
The UPDATE statement is used to modify the existing data in a table.
It is used to change the values of one or more columns in a table based on specified
conditions.
For example:
3. ALTER statement
The ALTER statement is used to modify the structure of database objects, such as
tables, views, or schemas.
It can be used to add, modify, or drop columns, constraints, or indexes in a table.
For example:
ALTER TABLE Employees ADD Email VARCHAR(255);
4. SELECT statement
The select command is used to select data from a database.
Eg:
SELECT EmployeeName, EmployeeID, City FROM Employees;
SELECT * FROM Employees;
5. FROM clause
The FROM command is used to specify which table to select or delete data from.
Eg: SELECT EmployeeName, City FROM Employees;
6. The WHERE clause is used to filter records. It is used to extract only those
records that fulfill a specified condition.
Eg:
SELECT * FROM Employees WHERE City = 'delhi';
SELECT EmployeeName, EmployeeID FROM Employees WHERE Empsal >
= 55000;
The WHERE clause can be combined with AND, OR, and NOT operators.
7. The AND and OR operators are used to filter records based on more than
one condition:
8. DELETE statement
The DELETE statement is used to remove one or more rows from a table based on
specified conditions.
It deletes specific rows of data while keeping the table structure intact.
Q1: Consider the following MOVIE table and write the SQL queries based on it.
(b) List business done by the movies showing only MovieID, MovieName and Total_Earning.
Total_Earning to be calculated as the sum of ProductionCost and BusinessCost.
(d) Find the net profit of each movie showing its MovieID, MovieName and NetProfit. Net
Profit is to be calculated as the difference between Business Cost and Production Cost.
(e) List MovieID, MovieName and Cost for all movies with ProductionCost greater than
10,000 and less than 1,00,000.
(f) List details of all movies which fall in the category of comedy or action.
(g) List details of all movies which have not been released yet.
Answers:
(a) Display the MatchID of all those matches where both the teams have
scored more than 70.
Answer:
SELECT MatchID FROM MATCH_DETAILS WHERE FirstTeamScore >
70 AND SecondTeamScore > 70;
(b) Display the MatchID of all those matches where FirstTeam has scored
less than 70 but SecondTeam has scored more than 70.
Answer:
(c) Display the MatchID and date of matches played by Team 1 and won
by it.
Answer:
SELECT MatchID, MatchDate FROM MATCH_DETAILS WHERE FirstTeamID = 1 AND
FirstTeamScore > SecondTeamScore;
(d) Display the MatchID of matches played by Team 2 and not won by it.
Answer:
SELECT MatchID FROM MATCH_DETAILS WHERE SecondTeamID = 2 AND SecondTeamScore <= FirstTeamScore;
Question 3
AVG()
The AVG() function returns the average value of a numeric column.
Syntax:
SELECT AVG(column_name) FROM table_name WHERE condition;
SUM()
The SUM() function returns the total sum of a numeric column.
Syntax:
SELECT SUM(column_name) FROM table_name WHERE condition;
The SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
Example
COUNT() Example
MIN() Example