0% found this document useful (0 votes)
38 views

Basic Syntax

The document discusses various SQL queries and functions including selecting columns, filtering rows, aggregation, formatting dates, concatenation, and joining tables. Common queries include selecting specific columns, filtering by conditions, sorting, limiting results, and aggregating values. Functions covered are count, sum, format, datediff, datedadd, concat, and substring.

Uploaded by

Mohamed Mougi
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Basic Syntax

The document discusses various SQL queries and functions including selecting columns, filtering rows, aggregation, formatting dates, concatenation, and joining tables. Common queries include selecting specific columns, filtering by conditions, sorting, limiting results, and aggregating values. Functions covered are count, sum, format, datediff, datedadd, concat, and substring.

Uploaded by

Mohamed Mougi
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Usage

Retrieve specific columns from a table in specific data base


Change the column name to other name
Retrieve all columns from a table in data base
For filtration
Add comments

Retrieve both words in column name


Retrieve all except both words in column name

Retrieve all the records which starts with a and it can be not like
percentage is a wildcard means anything starts with it can be also ‘%a’
or ‘%a%’
Use operators

Get the unique Values

Skip rows
Use left join or right join instead

if posting_date is a timestamp with a value like ‘2024-03-07 14:30:00’,


casting it to a date posting_date ::date would result in ‘2024-03-07’,
allowing for an accurate comparison with other date values.
SQL Query
Select column1, column2 from Database.Table name
Select column name 1 as ‘Alien name’ from database. Table name
Select * from database. Table name
Select * from database. Table name where column name = ‘word’
Use - - or \* */
<> means not equal
Select * from database. Table name where column name = ‘word 1’ Or column name = ‘word 2’
Select * from database. Table name where column name = ‘word 1’ and column name = ‘word 2’
Select * from database. Table name where column name in (‘word 1’, ‘word 2’)
Select * from database. Table name where column name not in (‘word 1’, ‘word 2’)
Select * from database. Table name where column name = ‘word 1’ and (column name = ‘word 2’ or column name = ‘word
Select * from Database.Table name where column name like ‘a%’

Select * from database. Table name where Column total > 1000
Select * from database. Table name where Column total Between 1000 and 5000
Select top (100) * where (column) is not null
Select top (100) * where (column) is null
SELECT Student Name from Students WHERE NOT Grade IS NULL
SELECT Product Name from Inventory WHERE NOT ProductStatus = 'Discontinued'
SELECT Order ID from Orders WHERE NOT (Order Amount >= 100 AND Order Amount <= 500)
Select distinct column1, column2 from database. Table name
Select * from database.table name order by (column name)
SELECT column1, column2, column3 FROM table ORDER BY column1 ASC, column2 DESC
SELECT Product Name, Unit Price from Products ORDER BY UnitPrice DESC LIMIT 5
SELECT column1, column2 FROM table LIMIT n OFFSET m
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName, Customers.Email FROM Orders JOIN Customers
SELECT EmployeeID, FirstName, LastName FROM Employees UNION SELECT EmployeeID, FirstName, LastName fro
SELECT CustomerID, CustomerName, ContactEmail from Customers UNION ALL SELECT SupplierID, SupplierName,
Usage
Can be replaced with AVG SUM Min Max
To count the number of rows in the query
To change the format of the date column and we must use alien
SQL Query
Select count (column name)
select count (*) from table
Select * from database.table name format (column date, ‘yyyy,mm,dd’)
Select * from database.table name Datediff (dd, start date, end date)
Select * from database.table name datedadd (ww, +5(-5), date)
Select concat (column1,” “, column2)
Select substring(column name, starting from, how many characters)
Select sum(column name) from database.table name order by (column name)
Select sum(column name) from database.table name group by (column name)
SELECT CustomerID, SUM(TotalPrice) AS TotalSpent from orders table GROUP BY CustomerID HAVING SUM(TotalP
SELECT ProductID, UnitPrice, Quantity, (UnitPrice * Quantity)

You might also like