• Shaarli
  • Tag cloud
  • Picture wall
  • Daily
  • RSS
  • Login
4252 shaares
2 / 4
Filters
71 results tagged sql

How to use INFORMATION_SCHEMA Views in SQL Server

QRCode

INFORMATION_SCHEMA Views allow you to find what tables are in your schema.

Search for varchar(max) and nvarchar(max)

select * from INFORMATION_SCHEMA.COLUMNS
where CHARACTER_MAXIMUM_LENGTH = -1

https://chartio.com/learn/databases/using-information-schema-views-to-check-to-see-if-table-exists-in-sql-server/
March 21, 2022 at 1:29:53 PM EDT *
sql mssql
FILLER

Questions About Pivoting Data in SQL Server You Were Too Shy to Ask - Simple Talk

QRCode

Of all the basic SQL operations, the pivot seems to cause the most problems. We can tell from the way that old articles on Simple-Talk on the topic continue to be read. It turns out that there are several questions that come to mind while learning about pivoting, but which are seldom asked on forums. Once more, Robert Sheldon attempts to answer these unspoken questions.

https://www.red-gate.com/simple-talk/databases/sql-server/t-sql-programming-sql-server/questions-about-pivoting-data-in-sql-server-you-were-too-shy-to-ask/
January 24, 2022 at 10:47:56 AM EST *
sql mssql
FILLER

How to Get NULLs Horribly Wrong in SQL Server - Simple Talk

QRCode

NULLs in SQL are a mixed blessing. The Three-Valued Logic of SQL has its uses but can cause difficulties to anyone who assumes that NULL has a 'value'. It can make reports go horribly wrong in a number of alarming ways, as Robert Sheldon explains.

https://www.red-gate.com/simple-talk/databases/sql-server/t-sql-programming-sql-server/how-to-get-nulls-horribly-wrong-in-sql-server/
January 18, 2022 at 11:18:15 AM EST *
mssql sql
FILLER

6 SQL Queries Every Data Engineer Should Be Aware of

QRCode

Running Totals

SELECT id,month
 , Amount
 , SUM(Amount) OVER (ORDER BY id) as total_sum
FROM bill

Common Table Expressions

WITH idtempp as (
  SELECT id as id
  FROM id 
  WHERE country = "US"
  AND status = "Y"
)

SELECT *
FROM bill
WHERE id in (SELECT id from idtempp)

Temporary Functions

Temporary functions allow you to modify the data easily without writing huge case statements.

CREATE TEMPORARY FUNCTION get_gender(type varchar) AS (
   CASE WHEN type = "M" THEN "male"
        WHEN type = "F" THEN "female"
        ELSE "n/a"
   END
)
SELECT 
  name,
  get_gender(Type) as gender
FROM bill
https://betterprogramming.pub/6-sql-queries-every-data-engineer-should-be-aware-of-2d0a2cc5986e
November 1, 2021 at 10:22:02 AM EDT *
sql mssql
FILLER

Fundamentals of table expressions, Part 6 – Recursive CTEs - SQLPerformance.com

QRCode

Continuing a series on table expressions, Itzik Ben-Gan explains recursive CTEs, features from the standard T-SQL is lacking, and workarounds.

Remember that the syntax of a query against a CTE is as follows:

WITH <CTE name> [ ( <with column list> ) ]
AS
(
<table expression>
)
<outer query>;
https://sqlperformance.com/2020/09/t-sql-queries/fundamentals-of-table-expressions-part-6-recursive-ctes
August 16, 2021 at 8:45:18 AM EDT *
mssql sql
FILLER

q - Text as Data

QRCode

Text as Data - q is a command line tool that allows direct execution of SQL-like queries on CSVs/TSVs (and any other tabular text files).

http://harelba.github.io/q/
July 2, 2021 at 11:16:54 AM EDT *
sql
FILLER

List table columns in SQL Server database - SQL Server Data Dictionary Queries

QRCode

Useful T-SQL queries for SQL Server to explore database schema.

select schema_name(tab.schema_id) as schema_name,
    tab.name as table_name, 
    col.column_id,
    col.name as column_name, 
    t.name as data_type,    
    col.max_length,
    col.precision
from sys.tables as tab
    inner join sys.columns as col
        on tab.object_id = col.object_id
    left join sys.types as t
    on col.user_type_id = t.user_type_id
order by schema_name,
    table_name, 
    column_id;
https://dataedo.com/kb/query/sql-server/list-table-columns-in-database
April 26, 2021 at 1:28:17 PM EDT *
sql mssql
FILLER

Admin Alert: One Common Cure for SQL0901 Package Errors - IT Jungle

QRCode

The SQL0901 error is an incredibly frustrating error that occurs with i5 ODBC, OLE DB, and JDBC connections. Because SQL0901 is a general SQL error, it can be difficult to diagnose and harder to solve. This week I will look at one specific SQL0901 scenario, where the failure occurs inside SQL’s extended dynamic package support,

https://www.itjungle.com/2006/09/13/fhg091306-story03/
March 22, 2021 at 10:08:01 AM EDT *
as400 sql
FILLER

SQL “Lateral Join” … do you know this SQL join? (EN) – BlogFaq400

QRCode

Lateral Join SQL DB2 for i: not everybody knows about it but it’s very powerful in our queries.
We want to get out one row only for each carmaker with the most sold model.

CROSS JOIN LATERAL:

SELECT a.Brand, Country, Modell, Year, Quantity FROM Faq400.AutoBrands A 
cross JOIN LATERAL 
(SELECT * from faq400.AutoStats stat
WHERE Year=2018 and A.idBrand=stat.IdBrand
order by Quantity desc
FETCH FIRST 1 ROW ONLY
) B ;
https://blog.faq400.com/en/db2-for-i/sql-lateral-join-db2-for-i-en/
February 9, 2021 at 1:25:09 PM EST *
sql as400
FILLER

Views in SQL Server – {coding}Sight

QRCode

In this article, we briefly covered types of views in SQL Server and gave several examples of user defined views.

Creating a View Across Instances

https://codingsight.com/views-in-sql-server/
December 3, 2020 at 10:55:01 AM EST *
sql mssql
FILLER

Top Answers to 5 Burning Questions on SQL COALESCE Function – {coding}Sight

QRCode

In this article, you'll find the answers to the five most-asked questions about SQL COALESCE function in SQL Server. Explore useful examples.

The definition of COALESCE is an expression that returns the first non-null value from a list of values. The COALESCE syntax is:

COALESCE ( expression [ ,…n ] )

(4) SQL COALESCE is Converted to CASE. ISNULL Stays ISNULL

COALESCE returns the data type of the value with the highest precedence.

https://codingsight.com/top-answers-to-5-burning-questions-on-sql-coalesce-function/
December 3, 2020 at 10:53:21 AM EST *
sql mssql
FILLER

Please stop using this UPSERT anti-pattern - SQLPerformance.com

QRCode

Locating the row to confirm it exists, only to have to locate it again in order to update it, is doing twice the work for nothing.

BEGIN TRANSACTION;

UPDATE dbo.t WITH (UPDLOCK, SERIALIZABLE) SET val = @val WHERE [key] = @key;

IF @@ROWCOUNT = 0
BEGIN
  INSERT dbo.t([key], val) VALUES(@key, @val);
END

COMMIT TRANSACTION;
https://sqlperformance.com/2020/09/locking/upsert-anti-pattern
September 8, 2020 at 2:57:38 PM EDT *
mssql sql
FILLER

What columns are in that index? - Erin Stellato

QRCode
  • Whenever you know that the column, or combination or columns, makes a unique key for a nonclustered index, I always recommend that you specify UNIQUE when you create the index. That information is great for the optimizer to have when generating query plans.

  • Think carefully about the impact of your clustering key on your nonclustered indexes. Any non-unique nonclustered index will be larger (more pages, eventually more levels) because the entire clustering key will be in the tree level.

  • If you have a clustering key with a column (or multiple columns) that are frequently modified, then those modifications have to be propagated to the tree levels of the nonclustered indexes as well.

https://www.sqlskills.com/blogs/erin/what-columns-are-in-that-index/
August 21, 2019 at 8:57:01 AM EDT *
sql mssql
FILLER

T-SQL: Deleting all duplicate rows but keeping one [duplicate]

QRCode

You didn't say what version you were using, but in SQL 2005 and above, you can use a common table expression with the OVER Clause. It goes a little something like this:

WITH cte AS (
SELECT[foo], [bar],
row_number() OVER(PARTITION BY foo, bar ORDER BY baz) AS [rn]
FROM TABLE
)
DELETE cte WHERE [rn] > 1

https://stackoverflow.com/questions/6025367/t-sql-deleting-all-duplicate-rows-but-keeping-one
February 7, 2019 at 10:42:03 AM EST *
sql mssql
FILLER

Nullable columns and performance

QRCode

NULL values are special. For instance, a comparison between two NULL values, or even a comparison between a NULL value and a non-value, will always be false. This is because NULL values aren’t real values as such, but rather “unknowns”.

https://sqlsunday.com/2019/01/03/nullable-columns-and-performance/
January 7, 2019 at 10:46:57 AM EST *
sql mssql
FILLER

When should a primary key be declared non-clustered?

QRCode

Say that I have a table of People, and these people have a Country column and a unique Primary Key. It's a demographics table, so these are the only things I care about; what Country and how many unique people are tied to that country.

I am thus only ever likely to SELECT WHERE or ORDER BY the Country column; a clustered index on the Primary Key doesn't do me any good, I'm not accessing this data by PK, I'm accessing it by this other column. Since I can only have one clustered index on a table, declaring my PK as Clustered would prevent me from using a Clustered Index on Country.

https://dba.stackexchange.com/questions/7741/when-should-a-primary-key-be-declared-non-clustered
August 6, 2018 at 9:24:03 AM EDT *
sql mssql
FILLER

CMU Database Systems - 03 Advanced SQL (Fall 2017)

QRCode

CMU Database Group
Published on Sep 9, 2017
SUBSCRIBE 3.2K
Subscribe Subscribed Unsubscribe
Slides PDF: http://15445.courses.cs.cmu.edu/fall2...
Notes PDF: http://15445.courses.cs.cmu.edu/fall2...
Annotated Video: https://scs.hosted.panopto.com/Panopt...

Andy Pavlo (http://www.cs.cmu.edu/~pavlo/)
15-445/645 Intro to Database Systems (Fall 2017)
Carnegie Mellon University

https://www.youtube.com/watch?v=6dg6fvJbqu8
January 24, 2018 at 9:19:18 AM EST *
sql
FILLER

What is a Key Lookup? - theBoredDBA.com

QRCode

Therefore I thought I would give a quick explanation for anyone still unsure what a Key Lookup is actually doing and why it’s considered bad.

We all know that Non-Clustered indexes all include the “Clustered Key” of the table. If not, then you do now. And if you have no Clustered Key (ie. Your table is a Heap) then the non-clustered will contain a Row Identifier which is unique and refers back to a specific record within your Heap. This is how a non-clustered index is linked to your table… whether it be via the Clustered Key or the RID.

http://www.theboreddba.com/Categories/indexes/What-is-a-Key-Lookup.aspx
January 17, 2018 at 4:40:08 PM EST *
mssql sql
FILLER

What’s an “Index” and how do they work?

QRCode

We often hear indexes explained using the analogy of an index in the back of a book. You want to find the information about “rabbits” for instance – and so you look that up in the back and find the list of pages that talk about rabbits.

https://matthewmcgiffen.com/2017/06/12/what-is-an-index/
June 19, 2017 at 10:47:06 AM EDT *
sql mssql
FILLER

COALESCE vs. ISNULL T-SQL Functions | T-SQL content from SQL Server Pro

QRCode

The COALESCE and ISNULL T-SQL functions are used to return the first nonnull expression among the input arguments. SQL Server practitioners often wonder what the difference is between the two functions.

http://sqlmag.com/t-sql/coalesce-vs-isnull
June 7, 2017 at 9:41:40 AM EDT *
sql mssql
FILLER
2 / 4
Shaarli · The personal, minimalist, super fast, database-free, bookmarking service by the Shaarli community · Documentation
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super fast, database-free, bookmarking service by the Shaarli community