Basic SQL Queries (P-1)
Basic SQL Queries (P-1)
Presented By
Md. Zahid Hasan
[email protected]
এই Lesson এ কি শিখব?
• Create and Delete Database
• Create and Delete Table
• Insert records Into Table
• Delete records into Table
• Update or Modify records into Table
• Read records from Table
• Where and Order By Clause
• Group By Clause
Create and Delete Database
online_event
Create Table
CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)
MyGuests
MyGuests
MyGuests
MyGuests
• The WHERE clause is used to extract only those records that fulfill
a specified condition.
Update or Modify records into Table
MyGuests
MyGuests
MyGuests
• The ORDER BY clause sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
MyGuests MyGuests
id firstname lastname id firstname lastname
Number Country
1 Germany
2 Mexico
1 UK
• COUNT
• SUM
• AVG
• MIN
• MAX
Why use aggregate functions
• From a business perspective, different organization levels have different
information requirements. Top levels managers are usually interested in knowing
whole figures and not necessary the individual details.
>Aggregate functions allow us to easily produce summarized data from our
database.
• For instance, from our test database , management may require following reports
> Least rented movies.
> Most rented movies.
> Average number that each movie is rented out in a month.
COUNT Function movierentals
• The DISTINCT
keyword that allows
us to omit duplicates
from our results.
SELECT `movie_id` FROM `movierentals`;
Output: 2003
MAX function
Movies
• The MAX function
Movie_Name year_released
returns the highest Sharlock holmes 2003
value in the Quarentine 2020
specified table field.
As an example, let's suppose we want to know the year in which
the recent movie in our library was released
Output: 2020
SUM function
payments
Output: 10500
AVG function
payments
• AVG function
returns the average
of the values in a
specified column.
Output: 3500
Mysql Aliases
payments
Select name from instructor where salary between 8000 and 12000;
instructor
instructor
Name salary
Name salary
Zahid 1000
Sobuj 8000
Morium 6000
Moin 11000
Bob 15000
Sobuj 8000
Moin 11000
“Success comes from having dreams that are bigger
than your fears.”
– Bobby Unser