0% found this document useful (0 votes)
4 views3 pages

SQL

SQL (Structured Query Language) is a programming language used for managing and querying databases. It includes various commands and functions for data retrieval, manipulation, and aggregation, such as SELECT, COUNT, SUM, and JOIN operations. The document outlines the syntax for SQL queries, key functions, and methods for performing operations like natural joins and division.

Uploaded by

Dishita Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

SQL

SQL (Structured Query Language) is a programming language used for managing and querying databases. It includes various commands and functions for data retrieval, manipulation, and aggregation, such as SELECT, COUNT, SUM, and JOIN operations. The document outlines the syntax for SQL queries, key functions, and methods for performing operations like natural joins and division.

Uploaded by

Dishita Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL

SQL (Structured Query Language) – A database programming language to


communicate between two programs (e.g. a website and a database).
‘<>’ is used to indicate ‘not equals to’.

Format of SQL queries:


SELECT [List of Attributes] – This is the list of attributes that the resulting table
will have.
FROM [List of Tables] – This is the list of tables the data is being pulled from.
WHERE [Conditions] – This is the list of conditions applied to the data.
GROUP BY [List of Attributes] – This is how the data gets grouped.
HAVING [CONDITION] – Same as WHERE but after data gets grouped, for each
group individually.

Aggregated functions:
 COUNT([Attribute]): Returns the number of values in the attribute.
 SUM([Attribute]): Returns the sum of all values in the attribute.
 MIN([Attribute]): Returns the minimum value in the attribute.
 MAX([Attribute]): Returns the maximum value in the attribute.
 AVG([Attribute]): Returns the average of the values in the attribute.
DISTICT can be applied to the attribute of the aggregate functions COUNT, SUM,
and AVG. It has no effect on MAX and MIN. For COUNT, unless DISTICT is used,
any valid attribute or * can be entered as the result will always be the same.

Alt method of doing MAX:


SELECT [List of attributes] FROM [TABLE] as T
WHERE NOT EXISTS (SELECT [Attribute] FROM [TABLE]
WHERE [Attribute] > T.[Attribute])
Replacing > with < will make it function as MIN.

Key words and meanings:


 NOT – Inverses true and false.
 [Condition] AND [Condition] – Checks if a pair of conditions are true.
 [Condition] OR [Condition] – Checks if one of two conditions are true.
 DISTINCT [List of attributes] – SQL statement only gives/checks unique
values for the attributes.
 [Table/Attribute] AS [Name] – renames a table/attribute for the returned
table.
 [Table 1] MINUS [Table 2] – Removes all tuple in [Table 1] that’s also in
[Table 2].
o Some systems use the key word ‘EXCEPT’ instead
 [Table 1] UNION [Table 2] – Gives a table that holds all tuple that’s in at
least one of the tables. Writing UNION ALL will allow duplicate data.
 [Table 1] INTERSECT [Table 2] – Gives a table that holds all tuple that’s
also within both tables at the same time.
 [List of Attributes] IN ([SQL statement]) – Checks if all listed attributes are
within the result of the given SQL statement. It’s also an alt way of doing
INTERSECT.
 [List of Attributes] NOT IN ([SQL statement]) – Checks if the listed
attributes aren’t within the result of the given SQL statement. It’s also an
alt way of doing MINUS.
 [Attribute] [Operation] SOME ([SQL statement]) – Checks if the operation is
true with at least one value from the matching attribute from the given
table via the SQL statement.
 [Attribute] [Operation] ALL ([SQL statement]) – Checks if the operation is
true with all value from the matching attribute from the given table via
the SQL statement.
 EXISTS ([SQL statement]) – Checks if the SQL statement returns a table
with at least one tuple.

Valid Methods of doing a Natural Join:


 SELECT [Table 1].[Key 1], [List of wanted/needed attributes]
FROM [List of tables]
WHERE [Table 1].[Key 1] = [Table 2].[Key 1]

Repeat the key part for each pair of tables being combined (2 tables, 1
key. 3 tables, 2 keys. Etc.).

 SELECT [List of wanted/needed attributes]


FROM [Table 1] INNER JOIN [Table 2]
ON [Table 1].[Key 1] = [Table 2].[Key 1]

 SELECT [List of wanted/needed attributes]


FROM [Table 1] LEFT OUTER JOIN [Table 2]
ON [Table 1].[Key 1] = [Table 2].[Key 1]

LEFT OUTER JOIN will also include Null values on the [Table 2] part of the
resulting table, which are caused when tuples from [Table 1] can’t be
matched to any tuple from [Table 2] via [Key 1].

Division:
Division in SQL works by taking a list of 2 attributes (X and Y) from one table
(T1), and a list of one of the 2 attributes (X) as another table (T2). We then group
each X attribute in T1 by their Y value and then we compare the X of each group
in T1 with the X in T2. The end result is a table that lists the Y of each group in T1
that had all the X values in T2
 (SELECT [Attribute] FROM [Table 1])
MINUS
SELECT [Attribute] FROM (
(SELECT * FROM (SELECT [ID] FROM [Table 1]), [Table 2])
MINUS
(SELECT * FROM [Table 1]))
 SELECT DISTINCT [Attribute 1]
FROM [Table 1]
WHERE [Attribute 1] NOT IN (
SELECT DISTINCT [Attribute 1]
FROM ((SELECT DISTINCT [Attribute 1] FROM
[Table 1]) AS T, [Table 2]
WHERE ([Attribute 1], [Attribute 2]) NOT IN
(SELECT * FROM [Table 1])
)

 SLELECT DISTINCT [Attribute 1]


FROM [Table 1]
WHERE NOT EXISITS (SELECT * FROM [Table 2]
WHERE NOT EXISTS (SELECT * FROM [Table 1] AS s
WHERE s.[Attribute 1] =
[Table 1].[Attribute 1] AND
s.[Attribute 2] =
[Table 2].[Attribute 2]
)
)

You might also like