Tutorial by Examples: clause

Consider the below list comprehension: >>> def f(x): ... import time ... time.sleep(.1) # Simulate expensive function ... return x**2 >>> [f(x) for x in range(1000) if f(x) > 10] [16, 25, 36, ...] This results in two calls to f(x) for 1,000 values of...
Optionals must be unwrapped before they can be used in most expressions. if let is an optional binding, which succeeds if the optional value was not nil: let num: Int? = 10 // or: let num: Int? = nil if let unwrappedNum = num { // num has type Int?; unwrappedNum has type Int print(&quo...
Steam has a games under $10 section of their store page. Somewhere deep in the heart of their systems, there's probably a query that looks something like: SELECT * FROM Items WHERE Price < 10
SELECT * FROM Employees WHERE ManagerId IS NULL This statement will return all Employee records where the value of the ManagerId column is NULL. The result will be: Id FName LName PhoneNumber ManagerId DepartmentId 1 James Smith 1234567890 NULL 1 SEL...
Prefer public class Order { public OrderLine AddOrderLine(OrderLine orderLine) { if (orderLine == null) throw new ArgumentNullException(nameof(orderLine)); ... } } Over public class Order { public OrderLine AddOrderLine(OrderLine orderLine) { ...
The for and while compound statements (loops) can optionally have an else clause (in practice, this usage is fairly rare). The else clause only executes after a for loop terminates by iterating to completion, or after a while loop terminates by its conditional expression becoming false. for i in r...
Use a subquery to filter the result set. For example this will return all employees with a salary equal to the highest paid employee. SELECT * FROM Employees WHERE Salary = (SELECT MAX(Salary) FROM Employees)
A subquery in a FROM clause acts similarly to a temporary table that is generated during the execution of a query and lost afterwards. SELECT Managers.Id, Employees.Salary FROM ( SELECT Id FROM Employees WHERE ManagerId IS NULL ) AS Managers JOIN Employees ON Managers.Id = Employees.Id ...
SELECT Id, FName, LName, (SELECT COUNT(*) FROM Cars WHERE Cars.CustomerId = Customers.Id) AS NumberOfCars FROM Customers
DELETE FROM `table_name` WHERE `field_one` = 'value_one' This will delete all rows from the table where the contents of the field_one for that row match 'value_one' The WHERE clause works in the same way as a select, so things like >, <, <> or LIKE can be used. Notice: It is necessa...
OPERATORS $postsGreaterThan = Post::find()->where(['>', 'created_at', '2016-01-25'])->all(); // SELECT * FROM post WHERE created_at > '2016-01-25' $postsLessThan = Post::find()->where(['<', 'created_at', '2016-01-25'])->all(); // SELECT * FROM post WHERE created_at < '2...
You can use subqueries to define a temporary table and use it in the FROM clause of an "outer" query. SELECT * FROM (SELECT city, temp_hi - temp_lo AS temp_var FROM weather) AS w WHERE temp_var > 20; The above finds cities from the weather table whose daily temperature variation i...
The following example finds cities (from the cities example) whose population is below the average temperature (obtained via a sub-qquery): SELECT name, pop2000 FROM cities WHERE pop2000 < (SELECT avg(pop2000) FROM cities); Here: the subquery (SELECT avg(pop2000) FROM cities) is used to s...
Subqueries can also be used in the SELECT part of the outer query. The following query shows all weather table columns with the corresponding states from the cities table. SELECT w.*, (SELECT c.state FROM cities AS c WHERE c.name = w.city ) AS state FROM weather AS w;
Sample Data: CREATE TABLE table_name ( id, list ) AS SELECT 1, 'a,b,c,d' FROM DUAL UNION ALL -- Multiple items in the list SELECT 2, 'e' FROM DUAL UNION ALL -- Single item in the list SELECT 3, NULL FROM DUAL UNION ALL -- NULL list SELECT 4, 'f,,g' FROM DUAL; -- NULL item...
In addition to functional constructs such as Try, Option and Either for error handling, Scala also supports a syntax similar to Java's, using a try-catch clause (with a potential finally block as well). The catch clause is a pattern match: try { // ... might throw exception } catch { case i...
Dim sites() As String = {"Stack Overflow", "Super User", "Ask Ubuntu", "Hardware Recommendations"} Dim query = From x In sites Select x.Length ' result = 14, 10, 10, 24 Query...
A HAVING clause filters the results of a GROUP BY expression. Note: The following examples are using the Library example database. Examples: Return all authors that wrote more than one book (live example). SELECT a.Id, a.Name, COUNT(*) BooksWritten FROM BooksAuthors ba INNER JOIN Aut...
We can use 1,2,3.. to determine the type of order: SELECT * FROM DEPT ORDER BY CASE DEPARTMENT WHEN 'MARKETING' THEN 1 WHEN 'SALES' THEN 2 WHEN 'RESEARCH' THEN 3 WHEN 'INNOVATION' THEN 4 ELSE 5 END, CITY IDREGIONCITYDEPARTMENTEMPLOYEES_N...
To get records having any of the given ids select * from products where id in (1,8,3) The query above is equal to select * from products where id = 1 or id = 8 or id = 3

Page 1 of 3