Learn SQl

Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon Jayed Hossain Jibon

01. Create database and drop database

Solution
create database learn_sql;
drop database learn_sql;

02. How to create table

Solution
CREATE TABLE Customers (
    CustomerID serial PRIMARY KEY,
    CustomerName VARCHAR ( 100 ) UNIQUE NOT NULL,
    ContactName VARCHAR ( 20 ) NOT NULL,
    Address VARCHAR ( 100 ) NOT NULL,
    City VARCHAR ( 100 ) UNIQUE NOT NULL,
    PostalCode VARCHAR ( 100 ) UNIQUE NOT NULL,
    Country VARCHAR ( 100 ) UNIQUE NOT NULL
);

03. How to drop table

Solution
drop table Customers;

04. Insert data into a table

Solution
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Jibon Ahmed', '01987132107', '69/A, Dhaka', 'Dhaka', '1200', 'Bangladesh');

--- Multiple value insert
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES 
('Jibon Ahmed',   '01987132107', '69/A, Dhaka', 'Dhaka', '1200', 'Bangladesh'),
('Jayed Hossain', 'Georg Pipps', 'Geislweg 14', 'Salzburg ', '5020 ', 'Austria');

SQL Query

Return all the columns from the Customers table

Syntax:
SELECT column1, column2, ...
FROM table_name; 
Solution
select * from customers

Return CustomerName, City from the Customers table

Solution
select CustomerName, City from customers

The SELECT DISTINCT statement is used to return only distinct

Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;
Solution
select count(DISTINCT country) from customers;

SQL WHERE Clause

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Select all customers from Bangladesh ?

SQL requires single quotes around text values
Solution
SELECT * FROM Customers
WHERE Country='Bangladesh';

Select all customers with a CustomerID greater than 80:

Solution
SELECT * FROM Customers
WHERE CustomerID > 80;

SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Sort the products from highest to lowest price:

Solution
SELECT * FROM Products
ORDER BY Price DESC;

Sort the products alphatbetically by ProductName:

Solution
SELECT * FROM Products
ORDER BY ProductName;

Sort the products by ProductName in reverse order

Solution
SELECT * FROM Products
ORDER BY ProductName DESC;

Selects all customer sorted by the Country and CustomerName

Solution
SELECT * FROM Customers
ORDER BY Country, CustomerName;

SQL AND Operator

Syntax :
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

Select all customers from Spain that starts with the letter 'G'

Solution
SELECT * FROM Customers
where Country = 'Spain' and CustomerName LIKE 'G%';

Selects all fields from Customers where Country is "Germany" AND City is "Berlin" AND PostalCode is higher than 12000

Solution
SELECT * FROM Customers
where Country = 'Germany' 
and City ="Berlin" 
and PostalCode > 12000;

Select all Spanish customers that starts with either "G" or "R"

Solution
SELECT * FROM Customers
where Country = 'Spanish'
(CustomerName LIKE 'G%' OR CustomerName LIKE 'R%')

SQL OR Operator

Syntax :
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

Select all customers from Germany or Spain

Solution
SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';

Select all Spanish customers that starts with either "G" or "R"

Solution
SELECT * FROM Customers
WHERE City = 'Berlin' OR CustomerName LIKE 'G%' OR Country = 'Norway';

⬆ Back to Top