SQL

SQL Syntax

In this post, I will explain the main syntax that you need to know before learning advanced commands.
Show databases; You can watch the video for better understanding.

we can select a database using the following command

use database name;

If you want to see the list of tables in a database

Show tables; 

To display columns from a specific table you use:

Select * from table name

to understand the table columns for that refer you use:

Describe table name;

To create a database for example a customer database:

create database SQL master;

To create a table inside a database:

create table customers (Name varchar(25), Age int, Sex char(1),Doj date, City varchar, Salary float);

-Add values to the customer table:

insert into customers  values (“Mohamed”, 30, M, 2021-09-01, Tunis, 5000),

(“Salah”, 25, M, 2021-08-01, Tunis, 4500),

(“Emna”, 30, F, 2021-05-01, Cairo, 3000),

(“Foued”, 22, M, 2020-09-01, Cairo, 5000),

(“Ali”, 45, M, 2019-07-01, Rabat, 5000);

You will get the following results

-Print unique values in city column:

Select distinct city from customers;

-Count  number of employees in a table:

select count(name) from customers

-you can give it another name

select count(name) as count_name from customers

-You want to get the total salaries:

Select sum (salary) from customers;

-You want to find the average salary:

Select avg (salary) from customers;

-You want to select specific columns

select from customers name age city;

-you want to find employees with age greater than 30:

select * from customers where age >30;

-You want to find only female employees:

select * from customers sex=’F’;

-You want to select employees from Tunis and Rabat:

select * from customers where

city=’Rabat’ or city = ‘Tunis’;

same result

select * from customers where

city in (‘Rabat’ or ‘Tunis’);

-Select values between two dates:

Select * from customers where

doj between ‘2020-1-1’ and ‘2020-12-31′;

-Select values specific condition

select *from customers where

age=30 and sex=’M’;

-Group by statement you want to find salaries by department

select sex sum(salary) as total_salary from customers

group by sex

-Order statement

select * from customers order by salary;

select * from customers order by salary desc;

-sum, sudtract,  function

select (10+20) as addition;

select (10-20) as subtract

-find a length of a string:

select length (‘Egypt’) as total_len;

-Repeat

select repeat (‘#’, 10);

-Convert a text to upper case or lower case:

Select upper(‘TuniSia’) as upper_case;

Select Lower(‘TuniSia’) as lower_case;

-select current date:

select curdate();

-extract day from current day:

select day(curdate());

-Select current day and time:

Select now();

 

 

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button