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

SQL Learning

sql spell

Uploaded by

Suman Kumari
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL Learning

sql spell

Uploaded by

Suman Kumari
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 22

Database(postgress)

· what are databases


· why use databases.
· Databases are systems that allow users to store and
organise data.And they're useful when dealing with large
amounts of data.
· So you might be wondering what do the typical users
· look like who deal with databases.
· Well databases actually have a wide variety of users.These
can include analysts such as a marketing analyst,
· a business analyst or a sales or sales operation analyst;
· or more technical people like data scientists,
· software engineers, or web developers.
· Basically anyone needing to deal with data
· has a great use case to work with a database.
· So since most users have some familiarity
· with a spreadsheet software, either Google Spreadsheets
· or Microsoft Excel, what we're gonna do is discuss
· the transition from going from spreadsheets to
databases.
· So let's go ahead and discuss some use cases
· for spreadsheets versus use cases for databases.
· And then leverage that knowledge
· to better understand databases.
· So spreadsheets are really good for a one-time analysis
· or to quickly need to chart something out,
· such as an Excel.
· And they work with reasonable data set sizes,
· something that Excel and Google Spreadsheet
· can open up really easily.
· And what's really nice about spreadsheets is they
· allow the ability for untrained people to work with data.
· So anyone can just double click an Excel file,
· and open it and work with it.
· However, they do have downsides
· where databases really shine.
· Databases are really great for data integrity
· meaning you don't want to have someone
· be able to just click on a cell and change the data.
· Databases don't really allow for that.
· And they can also handle massive amounts of data.
· Data sizes that would just basically make Excel
· or Google Spreadsheets crash.
· And they can also quickly combine different datasets
· something that might be difficult to do for larger datasets
· in a spreadsheet.
· They also are perfect for automating steps for re-use
· due to the sequel syntax, which we'll learn about later.
· They can also support data for websites and applications.
· So most websites and web applications,
· or mobile applications, are linked to some sort of database
· to store their data and retrieve their data.
· So let's go ahead and leverage that knowledge
· from spreadsheets to understand databases
· and see the similarities between them.
· So here we have a picture of a
· looks like a normal spreadsheet.
· Here we have the columns.
· It's just a basic glucose-tracking spreadsheet.
· And we have columns such as time, some measurements
· et cetera, and various rows with different data points.
· And then at the bottom here we have tabs
· which are referencing various sheets
· in the spreadsheet software.
· Here we're using Excel.
· So when we're transitioning from a spreadsheet
· to a database, we can think of a database's tables
· as the tabs of a spreadsheet.
· So those tabs of a spreadsheet handles various types of
data
· or different rows in different columns.
· Those tabs will translate to tables in a database.
· So that a database is made up of many tables.
· And then those tables are made up of rows and columns.
· So the columns in the table of a database
· is basically exactly what you would think
· as a column in a spreadsheet.
· It's just a vertical column that denotes
· a certain data type.
· So for instance the first one here we have time.
· Second one we have measurements.
· So columns in spreadsheets are basically the same
· as columns in the table of a database.
· And then the other feature we have from a spreadsheet
· to a database is going to be rows.
· And rows again behave exactly like you would expect them
to
· in a database table.
· They're just the actual row of all that data
· for a certain data point.
· So in this case we're highlighting
· the data points seven eight and nine,
· and then all the measurements for those columns.
· So again databases instead of tabs
· they're gonna have tables, and those tables
· are also gonna have rows and columns.
· And you can basically just imagine them as spreadsheets,
· just much larger that you can't individually click on.
· Okay, so again we have columns, rows,
· and instead of tabs we're gonna have tables.
· So what are our database platform options?
· Well, there's actually lots of database platform options.
· A lot more than what I've put here,
· but here are some of the main ones.
· You have PostgreSQL.
· You have things such as MySQL, and MS SQL Server
Express,
· Microsoft Access, and then SQLite.
· Each of these has their pros and cons.
· But were gonna be focusing on PostgreSQL.
· The reason for this is because PostgreSQL is free,
· it's open source, it's widely used on the internet,
· it's multi platform so meaning if you have
· various operating systems, like Windows or Mac users,
· you can all use PostgreSQL.
· And one of my main reasons for choosing it
· is that it's super easy to instal
· and just hit the ground running.
· So PostgreSQL is a great database choice
· for learning how to use SQL.
· And SQL is a Structured Query Language,
· which is what it stands for.
· And it's what we're gonna learn in this course.
· SQL is gonna be the way we communicate with our
database.
· It's the way we're gonna be able to communicate
· with a database to get data out of it
· or put data into it.
· And the SQL learned in this course can be applied
· to a variety of databases or SQL based software.
· So we're not gonna pigeonhole ourselves to just
PostgreSQL.
· What you learn in this course, you're gonna be able
· to apply it to a variety of databases
· or software that uses SQL.
· So for example of what a general SQL syntax would look
like.
· It would look something like this.
· Select some column names from a table
· and then you have some functions or clauses.
· Here I'm ordering by the first name.
· So we're gonna learn how to write and use SQL syntax
· like this in this course.
· So SQL is actually useful for a lot of things.
· Everything I've listed here runs on some sort
· of syntax of SQL.
· So you have PostgreSQL or MySQL or Oracle Databases,
· Microsoft Access, et cetera, the basic databases
· you would think of that can use SQL.
· But there's also a lot of software out there that uses SQL
· as a way of interacting with their data, such as Looker,
· or Periscope Data.
· And there's also some abstractions that run on top
· of big data systems, such as Hadoop.
· You can use SQL with Hive or Google's BigQuery
· or Facebook's Presto which is basically using SQL
· to run on top of very large data systems
· that use distributed data.
· So SQL's gonna be incredibly useful
· to operate a variety of tools and databases with.
· So you're also gonna be notified if any of the SQL
commands
· we learned in this course are specific
· to the PostgreSQL environment.
· Most of the commands we learn are actually just gonna be
· general SQL, so you won't get too many
· of these notifications.
· Basically everything you learn here, you're gonna
· be able to apply directly to any conventional SQL database.

Select Statement

· Select Statement.
· Select is the most common statement used in SQL, and it
fundamentally allows us to retrieve information from a
table that exists within a database.
· SELECT COLUMN_NAME FROM TABLE_NAME;
· Sql challenes we have a scenerio were we want to retrieve
information form Customer table , its first name , last
name , and email address
· SELECT first_name, last_name,email FROM Customer;
DISTICT KEYWORD
· Sometimes a table contains a column that has duplicate
values, and you may find yourself in a situation
· where you only want to list the unique or distinct values.
· In that column, the distinct keyword can be used to return
only two distinct values in a column.
· SELECT DISTICT column FROM table;
· Sometimes to clarify which column distinct is being applied
to.
· You could also add in parentheses for clarity.
· SELECT DISTICT(column) FROM table;
· So note that distinct on a column works with or without
parentheses.
· name choice
· ansu reading
· ansu watching movie

COUNT

SELECT COUNT(column) FROM table;


or
SELECT COUNT(*) FROM table;

· The count function returns the number of input rows that


match a specific condition of a query.
· We can apply count on a specific column or just past count
and then an asterisk.
· This should return the same result because count is
merely reporting back the number of rows returned,
· which is going to be the same regardless of what actual
column you're looking at.
· Notice here Count does need the parentheses because
it's a function acting on something so it won't work
without the parentheses.
· Combination of count and distict
· Imagine we wanted to know how many unique names are
there in the table.
· So to answer this question, we need to first decide the
distinct number of names.
· Then we'll provide count on that result of distinct name
from table.
· So the logic that's going to happen is distinct is going to
figure out what the distinct names are.
· SELECT COUNT(DISTICT name) FROM table;
· So the WHERE statement allows us to specify conditions on
columns for the rows to be returned.
· The basic syntax would look something like this.
· We decide on what columns we want back, so we'll say
select column one, column two, etc. and then
· we decide what table we want those columns from.
· And then after that we can describe conditions.
· So we'll say where and then some manner of conditions.
· So let's actually go into detail of what those conditions are
like.
· The Where clause appears immediately after that from
clause of the select statement and then the conditions are
used to filter the rows returned from the Select
Statement.
· And PostgreSQL provides a variety of standard operators to
construct those conditions.
· And I want to make it clear that the conditions we show
here are available in any major SQL engine.
· So it's not just unique to PostgreSQL.
· So we first have the comparison operators.
· And the comparison operators allow us to compare a
column value to something essentially allowing us
· to answer questions or queries such as where is the price
greater than $3?
· Or Where is the pet's name equal to Sam?
· So those kind of statements of greater than or equal to or
less than those are the comparison operators.
· And you're probably familiar with the base syntax
operators, at least the operator syntax signs, because
· they're just basic math.
· So for example, if we want to check for equality, we just
use the equal sign.
· Then we also have the greater than and less than sign, the
greater than equal to signs and then even
· the not equal to.
· And there's two ways of doing not equal to in PostgreSQL
you can have a less than greater than sign
· right next to each other or the more common way is the
new exclamation mark and then an equal sign.
· In addition to those, we have the logical operators and that
allows us to combine multiple comparison
· operators or effect comparison operators.
· So you're also probably familiar with these ideas of and or
and not and they essentially allow you to
· use multiple comparison operators so you can do
something like condition one has to be true and condition
· two has to be true or condition one is true or condition
two, two is true.
· And then you have the non operator, which essentially
means the opposite of whatever condition you're
specifying.
· So where this condition is not true.
· So there's a condition there where I want the name
column to be equal to the string.
· David.
· So the way I do this is I say select the name column and
the choice column from table where name is
· equal to David.
· And note here PostgreSQL use single quotes in order to
denote these strings.
· And also when we're checking for equality here, the
capitalization does matter.
· Also keep in mind that I selected both the name and the
choice column here.
· I could, if I wanted to, just select the name column from
table where name is equal to David.
· And then I would just get back the entries on the name
column.
· So that's not terribly useful.
· So keep in mind, you're probably going to ask for more
than just a column where you're filtering these
· conditions on.
· Just as asking for name and choice.
· Let's imagine I wanted to do multiple conditions.
· So previously I have two Davids here where we have green
and red.
· I can use those logical operators such as the and keyboard
to test for multiple conditions.
· So here I'm grabbing the name and choice column from
this table where the name is equal to David and
· the choice is equal to red, returning back this single row
where both those conditions have to be true.
· Okay, let's go ahead and see you in the next lecture in part
two where we jump to pkg admin and

SELECT name , choice FROM table WHERE name='ansu'


AND choice='Red';
· SELECT 8 FROM film WHERE renta_cost > 4
AND replacment_cost >= 0.9 ;
· SELECT count(rating) FROM film
WHERE rating= 'A' OR rating='B';
· SELECT count(rating) FROM film
WHERE rating != 'A;
challenge

Q.1) what is email of customer of f_name ansu and last_name


prasad;
SELECT email FROM customer
WHERE f_name = 'ansu' AND last_name='prasad';

Q.2) Could you give them the description for the movie Outlaw
Hanky?
SELECT decription FROM film WHERE movie='Outlaw Hanky';

Q.3) three is a customer is late on their movie return and we've


mailed them a letter to their address at 259 iPod Drive.And we
should also call them on the phone to let them know.

SELECT email,phone FROM customer WHERE address='259 iPod


Drive' AND movie_title ='return';

ORDERBY

· You may have noticed that PostgreSQL sometimes returns


the same request query results in a different
· order, meaning you may have run the exact same query on
your computer and saw the results in a slightly
· different order than what I have on my computer.So in
order to alleviate this issue, if we want the results in a
certain order, we can use the ORDERBY we can use the
order
· by to sort rows based on a column value in either
ascending or descending order.
· BASIC SYNTEX

SELECT column1 , column2 FROM table ORDER BY


column1 ASC/DESC;
· Notice the order by towards the end of the query, since we
want to do any selection and filtering first before finally
sorting.
· use ASC to sort in ascending order the DSC to sort in
descending order.
· If you leave it blank order by just uses ascending by
default.
· SELECT comapny, name, sale FROM table ORDER BY
comapny, sale ;
· its means first its filter based on asc order of company
then sale
· LIMIT

The limit command allows us to limit the number of rows


returned for a query.
· It's useful for not wanting to return every single row in a
table, but only view maybe the top few
· Limit also becomes extremely useful in combination with
the order by statement
· Limit goes at the very bottom end of a query request and is
the last command to be executed.
· Because essentially what the limit does is after you're done
filtering with the wear statement, ordering
· with order by and sorting and any other sort of conditions
or any filters you want to do on your query,
· the limit is just going to say, all right, how many rows do
you want at the end of the day
· SELECT column1 , column2 FROM table ORDER BY
column1 ASC/DESC LIMIT 5; only get five rows
· SELECT * FROM payment WHERE amount != 0
ORDER BY payment_date DESC LIMIT 5;

Q) challenge task, we want to reward our first ten paying


customers.
So the question is, what are the customer IDs of the first ten
customers who ever created a payment?

SELECT customer_id FROM customer ORDER BY


payment_created ASC
LIMIT 10;

Q) customer wants to quickly rent a video to watch over their


short lunch break.What are the titles of the five shortest that is
in length of runtime movies?
SELECT title,length FROM movie ORDER BY length DESC
LIMIT 5;

Q) if that previous customer can watch any movie that is 50


minutes or less in runtime,how many options does she or he
have?

SELECT count(title) FROM film WHERE length >=50;

BETWEEN

· The between operator can be used to match a value


against a range of values.
· a value is greater than or equal to low and the value is less
than or equal to high.
· So note that the low and the high here are both inclusive,
so it's actually including the end points
· You can also combine the between keyword with the not
logical operator, essentially asking where is the value not
between low and high.
its not inclusive meaning not inclusde value start and end
point
· the between operator can also be used with dates.
· You should note that you need to format dates in this case
and the ISO 8601 standard format, which
· is essentially just YYYY-MM-DD four numbers for the year,
a dash, then two numbers for the month, a dash and
· date BETWEEN '2023-09-14' AND '2023-10-14'
· SELECT * FROM payment WHERE amount BETWEEN 8 AND
9
LIMIT 2;
· opposite
SELECT * FROM payment WHERE amount NOT BETWEEN 8
AND 9
LIMIT 2;

IN
· you want to check for multiple possible value options, for
example, if a user's
· name shows up in a list of known names.
· So instead of having to write a bunch of or statements
such as Name is equal to David or Name is equal
· to Claire or name is equal to Zach, we could use the
operator to just create a condition that quickly
· checks to see if a value is included in a list of multiple
options

· general syntax

· value IN (option_1 , option_2 ......., option_n)


· SELECT colour FROM table WHERE colour IN('RED','BLUE');
· SELECT DISTINCT(amount) FROM payment ORDER BY
amount
· SELECT * FROM payment WHERE amount IN(10,20,30,40);
· SELECT count(*) FROM payment WHERE amount
IN(10,20,30,40);
· Opposite
SELECT count(*) FROM payment WHERE amount NOT
IN(10,20,30,40);
· SELECT * FROM customer WHERE first_name
IN('ansu','avi','suman');
·

You might also like