Untitled

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

TEST TIME ON JDBC

URL: https://forms.gle/42kCAXuYNud96huD9

QR CODE:
JDBC
Aggregate Functions
COUNT FUNCTION
COUNT function is used to Count the number of rows in a
database table. It can work on both numeric and non-numeric
data types.
SUM Function
Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.
AVG function
The AVG function is used to calculate the average value of
the numeric type. AVG function returns the average of all
non-Null values.
MIN Function

MIN function is used to find the minimum value of a certain column. This

function determines the smallest value of all selected values of a column.

MAX Function

MAX function is used to find the maximum value of a certain column. This

function determines the largest value of all selected values of a column.


EXAMPLE

PRODUCT COMPANY QTY RATE COST

Item1 Com1 2 10 20

Item2 Com2 3 25 75

Item3 Com1 2 30 60

Item4 Com3 5 10 50

Item5 Com2 2 20 40
QUERY

SELECT SUM(COST) FROM PRODUCT;


SELECT MIN(COST) FROM PRODUCT;
SELECT MAX(COST) FROM PRODUCT;
SELECT AVG(COST) FROM PRODUCT;
SELECT COUNT(QTY) FROM PRODUCT;
Select *from Product;
KEY CONSTRAINTS

Primary Key in DBMS

What is a Primary Key

A Primary Key is the minimal set of attributes of a table that has the task to

uniquely identify the rows, or we can say the tuples of the given particular

table.

Syntax

<column_name><datatype> Primary key;


EXAMPLE

For example: When we store the registration details of the


students in the database, we find the registration number
field unique and assign the primary key to the field. Also,
for an employee table, we set the primary key on the
employee Id of the table.
Creating a Primary Key

CREATE TABLE STUDENT_DETAIL (


Roll_no int NOT NULL PRIMARY KEY,
Name varchar (200) NOT NULL,
Marks int NOT NULL
} ;
FOREIGN KEY

The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.

A FOREIGN KEY is a field (or collection of fields) in one table, that refers
to the PRIMARY KEY in another table.

The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.
PERSON TABLE

PersonID LastName FirstName Age

1 Hansen Ola 30

2 Svendson Tove 23

3 Pettersen Kari 20
ORDER TABLE

OrderID OrderNumber PersonID

1 77895 3

2 44678 3

3 22456 2

4 24562 1
Creating a Primary Key

❖ The "PersonID" column in the "Orders" table points to the

"PersonID" column in the "Persons" table.

❖ The "PersonID" column in the "Persons" table is the PRIMARY

KEY in the "Persons" table.

❖ The "PersonID" column in the "Orders" table is a FOREIGN KEY

in the "Orders" table.

❖ The FOREIGN KEY constraint prevents invalid data from being

inserted into the foreign key column, because it has to be one

of the values contained in the parent table.


SQL FOREIGN KEY on CREATE TABLE

SQL Server / Oracle / MS Access:


CREATE TABLE Orders (
OrderID int NOT NULL PRIMARY KEY,
OrderNumber int NOT NULL,
PersonID
int FOREIGN KEY REFERENCES Persons(PersonID)
);
SQL FOREIGN KEY on CREATE TABLE

MYSQL:

CREATE TABLE Orders (

OrderID int NOT NULL,

OrderNumber int NOT NULL,

PersonID int,

PRIMARY KEY (OrderID),

FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)

);
JOINS

SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on
a related column between them.
Different Types of SQL JOINs
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either
left or right table
TYPES OF JOINS
INNER JOIN

The INNER JOIN keyword selects records that have matching values

in both tables.

INNER JOIN Syntax

SELECT column_name(s)

FROM table1

INNER JOIN table2

ON table1.column_name = table2.column_name;
SQL LEFT JOIN

The LEFT JOIN keyword returns all records from the left table

(table1), and the matching records from the right table (table2).

The result is 0 records from the right side, if there is no

match.

LEFT JOIN Syntax

SELECT column_name(s)

FROM table1

LEFT JOIN table2

ON table1.column_name = table2.column_name;
INNER JOIN

The INNER JOIN keyword selects records that have matching values

in both tables.

INNER JOIN Syntax

SELECT column_name(s)

FROM table1

INNER JOIN table2

ON table1.column_name = table2.column_name;
EXAMPLE-ORDER AND
CUSTOMER TABLE

OrderID CustomerID EmployeeID OrderDate ShipperID

10308 2 7 1996-09-18 3

10309 37 3 1996-09-19 1

10310 77 8 1996-09-20 2

CustomerID CustomerNam ContactName Address City PostalCode Country


e

1 Alfreds Maria Obere Str. Berlin 12209 Germany


Futterkiste Anders 57

2 Ana Ana Avda. de la México D.F. 05021 Mexico


Trujillo Trujillo Constitució
Emparedados n 2222
y helados

3 Antonio Antonio Mataderos México D.F. 05023 Mexico


Moreno Moreno 2312
Taquería
SQL QUERY JOIN

SELECT Orders.OrderID, Customers.CustomerName

FROM Orders

INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

SELECT Customers.CustomerName, Orders.OrderID

FROM Customers

LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID

ORDER BY Customers.CustomerName;
EXCERISE

1) Create two tables

2) Write a query for Right Join

3) Write a query for full Outer Join

4) Write a statement that will select the City column from the

Customers table.

5) Select all records where the City column has the value

‘chennai' and the PostalCode column has the value 600127.


EXERCISE

1)Select all records where the CustomerID column has the


value 32.
2)Use the NOT keyword to select all records where City is
NOT "Berlin".
SDOT TRAINING
PROGRAM QUESTION
Max Sliding Window

Given an array A and an integer K, the problem is to find the


maximum for each and every contiguous subarray of size k aka
the sliding window

There is a sliding window of size K which is moving from the


very left of the array to the very right.

You can only see the w numbers in the window. Each time the
sliding window moves rightwards by one position.

You have to find the maximum for each window.


Max Sliding Window

Problem Statement

Return an array C, where C[i] is the maximum value from A[i] to A[i+K-1].

If k > length of the array, return 1 element with the max of the array.

Example
Input: A[] = {8, 5, 10, 7, 9, 4, 15, 12, 90, 13}, K = 4
Output: {10, 10, 10, 15, 15, 90, 90}

Explanation
Maximum of first 4 elements is 10, similarly for next 4 elements (i.e from index
1 to 4) is 10, So the sequence generated is 10 10 10 15 15 90 90
Max Sliding Window

Illustration

Input: A[] = {1, 3, -1, -3, 5, 3, 6, 7}, K = 3


Output: {3, 3, 5, 5, 6, 7}

Explanation
Max Sliding Window

Brute Force Algorithm

The simplest approach to solve this problem is to iterate over


all possible sliding windows and find the maximum for each
window.

There can be a total of N – K + 1 sliding window and there are


K elements in each window.

1. Run a loop i from 0 to N – K + 1, denoting the current


window.
2. Initialise a variable max to INT_MIN to find the maximum
value of each window.
3. Run a nested loop from j from i to i + K to iterate over the
elements of the current window and maximize max.
4. Store the value of max for each window and return.
Max Sliding Window

Program: Brute force approach for Max Sliding Window

maxslidingwindow1.java

Sample IO
Input
arr = {1, 3, -1, -3, 5, 3, 6, 7} Time Complexity: O(N*k)
k = 3 Space Complexity: O(N – k + 1)
Output
3 3 5 5 6 7
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like