Single line comment in SQL Server
–
In an SQL statement, reserved words like
* clauses (SELECT, FROM, WHERE)
* built-in functions (LOWER)
* data types (VARCHAR)
are called _ .
keywords
Keywords form the syntax of SQL.
In an SQL statement, identifiers are _
names of column, table or schema.
SELECT name, LOWER(country) FROM customers WHERE country = "India"
Here, name, country, customer are identifiers.
In an SQL statement, the _ are used inside WHERE clause to compare values.
operators
An SQL statement is composed of clauses and each clause is composed of _
keywords, identifiers, operators and string literals.
In SQL statement, _ clause is used to filter columns of the table and _ clause is used to filter rows of the table.
List down some (as many as you can recall) clauses used in a SELECT query
The typical order of execution for major SQL clauses is:
* SELECT
* DISTINCT
* OFFSET / FETCH (pagination) / TOP
* FROM
* WHERE
* JOIN
* GROUP BY
* ORDER BY
* HAVING
SQL command to specify database in advance
USE databasename;
In the SQL query:
SELECT first_name, score FROM customers WHERE country = 'Germany';
the order of execution of clauses is
select all customers where score is NULL (unknown).
SELECT * from customers WHERE score IS NULL;
Which clause is used to sort the data?
ORDER BY <col-name>
By default, ordering sequence is ascending (ASC).
INSERT INTO customers(id,first_name, country, score)
VALUES (1, 'Maria', 'Germany', 350),
(2, 'John', 'USA', 900),
(3, 'Georg', 'UK', 750),
(4, 'Martin', 'Germany', 500),
(5, 'Peter', 'USA', 0),
(6, 'Sahil', 'India', null),
(7, 'Jonney', null, 600);
SELECT *
FROM customers
order by country DESC;Where will ‘Jonney’ be placed in the rows of result set?
last row
SQL treats NULL as the lowest possible value when sorting.
Write query for customers table to retrieve data ordered by ascending countries and within each country ordered by descending scores.
Assume country column has repetitive entries.
SELECT * FROM customers ORDER BY country ASC, score DESC
This is known as multi-level (or nested) sorting in SQL
Write query for customers table to retrieve data ordered by ascending countries and within each country ordered by descending scores.
Ensure null countries are skipped.
Assume country column has repetitive entries.
SELECT * FROM customers WHERE country IS NOT NULL ORDER BY country ASC, score DESC
(last/first)
We have some null entries in column A and we are sorting (order by) column A
SQL treats NULL as the lowest possible value when sorting.
(counts/ignores)
avg(score) will be _ .avg(score) will be _ .Find avg score and total number of customers for each country.
CREATE TABLE customers ( id int, first_name varchar(15), country varchar(15), score int );
select country, avg(coalesce(score, 0)) as avg_score_per_country, count(*) as count_per_country from customers where country is not null group by country
Write query to place NULL values in the end, while sorting on basis of score.
Hint: Maximum score value is 1000
select *, coalesce(score, 1001) as nscore from customers order by nscore asc;
Retrieve customers with non-null countries,
sorted by country in descending order.
SELECT * FROM customers where country is not null order by country DESC;
Which SQL clause helps aggregate data of a column by another column?
GROUP BY
it conveys the “for each” semantics.
For e.g., find total score for each country
Write SQL query to find the total score values for each country.
SELECT country, SUM(score)
FROM customers
GROUP BY country
If GROUP BY clause has two columns, then the _ is used as basis of aggregation.
combined value of two columns
Find total score and total number of customers for each country.
SELECT SUM(score) AS total-score, COUNT(*) AS total_customers FROM customers GROUP BY country;