DEV Community

soul-o mutwiri
soul-o mutwiri

Posted on

INTRODUCTION TO SQL

SQL databases are used in digital spaces for instance in commerce websbites, social media and other online spaces.
they help store and manipulate data, using sql queries, analyst can derive insights from the data stored in the data.

lets dive in and create a table, where the data is stored.

customers table showcasing table fields.

user_ID|firstname|lastname|age|city

SQl query to select all the data from this table looks like this:
select * from customers

it is important to note that SQL is case insensitive so
select = SELECT

  • A SELECT statement is used to select data from a table.

SQL query find the name and salary columsn from the Employees table:

select name, salary
from Employees;

SQL query to sort customers by their age and limit by the 2 oldest customers. in this query, where is used to filter out the rows which have no age value. Desc will be used to rank the oldest to the youngest.

select * from customers
where age is not null
order by age desc
limit 2

SQL query that selects the 3rd to 5th rows from the Employeees, ordered by the salary column in descending order.

select * employees
order by salary desc
limit 3 offset 2

STRING FUNCTIONS
there are some useful functions in sql to work with on text data

  1. we can use concat function to combine text from multiple columns.

select concat (first_name, '', lastname) as name
from customers
order by lastname desc;

  1. Lower and upper functions
    this convert the texts in the column to lowercase or uppercase respectively.
    select lower(firstname) from customers

  2. Substring function
    it allows you to extract part of the text in a column, it takes the starting position and the number of characters we want to extract
    to take the first 3 characters of the firstname
    select substring(firstname, 1,3)
    from customers;

Top comments (0)